Java Tic Tac Toe Program Code Using Arrays

Java Tic Tac Toe Program Code Using Arrays

This is a simple Tic-Tac-Toe game that uses a. Code: Can't Copy and Paste. This is a simple Tic-Tac-Toe game that uses a 2D array. Code an interactive, two-player game of Tic-Tac-Toe. You'll use a two-dimensional array of chars. Starter Code.

Java Tic Tac Toe Program Code Using Arrays

By Your mission, should you decide to accept it, is to create a Java program that can play a game of Tic-Tac-Toe with the user. As you probably know, Tic-Tac-Toe is a simple game usually played with paper and pencil. First, you make a simple 3 x 3 grid on the paper. Then two players alternate turns by marking Xs and Os in empty spaces on the grid.

The first player who makes three of his or her marks in a horizontal, vertical, or diagonal row wins. If all of the spaces in the grid are filled before any player marks three in a row, the game is a draw. Here are the rules and instructions for this challenge: • The computer plays against the human. The human moves first and is X.

The computer is O. • The program should begin by displaying a short welcome message, and then should prompt the player for his or her first move. For example: Welcome to Tic-Tac-Toe. Please enter your first move: • To designate the squares of the grid, use the letters A, B, and C for the columns and the numerals 1, 2, and 3 for the rows, like this: A1 B1 C1 A2 B2 C2 A3 B3 C3 Thus, to place an X in the top-left square, the human player would enter the text A1 when the program prompts the player for a move.

Torrent Ost Flashdance. After the human enters a move, the program should display the current status of the board on the console. • Use X to mark the human’s plays and O to mark the computer’s plays. Il Nuovissimo Manuale Dell.

Use vertical bar characters (found on the keyboard with the backslash character, just above the Enter key) and hyphens to draw the board in a simple grid. For example, if the user has entered A1 as his or her first move, the program would display the following: X --- --- --- --- --- --- • After the human’s move, the program determines its move, announces it to the user, displays an updated board, and then prompts for the user’s move. For example, you might see this: I will play A2: X O --- --- --- --- --- --- Please enter your next move: • Play continues until one player has scored three in a row or all squares have been filled with no winner. Your program must be able to determine whether either player has scored three in a row and won the game. (This is the most difficult part of this programming challenge.) • When the game is over, the program displays a message indicating the result of the game: “You beat me!” if the human player wins, “I beat you!” if the computer player wins, or “It’s a draw!” if the game ends in a draw. • The human and computer players can play in only those squares that are not already occupied by either player. • The program ends when the game is won by either player or the game is a draw.

If you want to play again, you must run the program again. Note that you are free to use any method you wish to determine how the computer should make its moves. I suggest you have the computer always play in the first empty square.

This is obviously not the best way to play Tic-Tac-Toe, and you will have no trouble beating the computer every time you play. But choosing this simple playing strategy allows you to focus on other aspects of the programming, such as how to internally represent the grid and how to determine when a player has won the game or when the game has ended in a draw. (In “Java Programming Challenge: Adding Arrays to the Simple Tic-Tac-Toe Program” you’re asked to come up with a better strategy for the program to determine its plays.) Here is an example of the console display for a complete game: Welcome to Tic-Tac-Toe. Please enter your first move: A1 X --- --- --- --- --- --- I will play at A2: X O --- --- --- --- --- --- Please enter your next move: B1 X O --- --- --- X --- --- --- I will play at A3: X O O --- --- --- X --- --- --- Please enter your next move: C 1 X O O --- --- --- X --- --- --- X You beat me!

Here are a few hints to get you started: • The best way to represent the grid is with an array. For now, use nine variables, named A1, A2, A3, B1, B2, B3, C1, C2, and C3. • If you use the int type for the grid variables, you can then use 1 to indicate that a square contains an X and 2 to indicate that a square contains an O. • Eight possible rows can lead to a win. A player wins if any of the following combinations of squares are either 1 (for X) or 2 (for O): A1 – A2 – A3 A1 – B1 – C1 A1 – B2 – C3 B1 – B2 – B3 A2 – B2 – C2 A3 – B2 – C1 C1 – C2 – C3 A3 – B3 – C3 • There are two ways to determine whether the game is a draw. The first is to check all nine squares: If none of the squares is empty and neither player has won, the game is a draw. The second is to count the moves: If nine moves have been made and neither player has won, the game is a draw.

You can find a solution to this programming challenge on the of the Java All-in-One For Dummies, 4th Edition product page.

Welcome to the Programming and Software Design Section, When asking for help with programming issues, please use the code tags to enclose your code, it makes things much more easily readable for the people trying to help you, thus improving your chances of actually getting help. To add code tags, click the button on the editor toolbar, then enter your code in the code editor that appears. If you are on a mobile device, or prefer to use BBCode, you can use [code] // Your code here // It will be syntax highlighted, though not necessarily corectly. [/code] (but the code editor is more consistent and less buggy). To guarantee a draw for O, however: • If X does not play center opening move (playing a corner is the best opening move), take center, and then a side middle. This will stop any forks from happening.

If O plays a corner, a perfect X player has already played the corner opposite their first and proceeds to play a 3rd corner, stopping O's 3-in-a-row and making their own fork. • If X plays center opening move, O should pay attention and not allow a fork. X should play a corner first. • If O takes center (best move for them), X should take the corner opposite the original, and proceed as detailed above. • If O plays a corner or side-middle first, X is guaranteed to win: • If corner, X simply takes any of the other 2 corners, and then the last, a fork. • If O plays a side-middle, X takes the only corner that O's blocking won't make 2 in a row. O will block, but the best of the other two will be seen by X, and O is forked.

The only way that X must lose is if O plays middle and then a side-middle.