Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Tic-Tac-Toe For this assignment, you will be creating an interactive Tic-Tac-Toe

ID: 3692305 • Letter: T

Question

Tic-Tac-Toe

For this assignment, you will be creating an interactive Tic-Tac-Toe game.

Turn this into a Java Program.

So let’s get started!

Part 1: Your Tic-Tac-Toe game!

You love playing tic-tac-toe, but don’t always have someone to play the game with. So, you decide to create your own that you can play against the computer.

This will be a standard tic-tac-toe game with X’s and O’s where the players alternate taking turns placing their tokens (i.e., X or O, depending on which one they are). It will include:

·         Standard 3 x 3 playing board (hint: use 2D arrays)

·         A winning game is either:

o   3 across

o   3 up and down

o   3 diagonally

·         Tie games are possible if a winning game is not achieved.

·         The game board with associated placement of the played X’s and O’s must be displayed after each turn.

Program Flow:

·         First, ask the user for their name and welcome them.

·         Then randomly decide who will go first, the user or the computer (and let the user know this)

·         The user and computer will alternate turns placing their tokens.

o   The user should be asked where they want to place their token

o   The computer should be asked to randomly choose an empty spot

·         The game ends when either a winning game occurs (see above) or all the spots are filled.

·         At the end of the game, ask the user if they would like to play again.

Input Options:

There are numerous options for getting input from users to indicate where they want their token placed.

Option 1: Ask them to give you the column and row number of their desired spot.

Option 2: Pre-fill each of the spots with a number, allowing users to select one number to indicate their desired spot. For example:

1 | 2 | 3

__|___|__

4 | 5 | 6

__|___|__

7 | 8 | 9

|   |

1.      Here is one more thing to do. Any input requested from the user and/or output received from the user should be in a window. At this point, you probably have your output going to the console. For your final submission, it needs to go to a window (JOptionPane). Don’t forget any additional libraries that you need to import to do this.

That’s it! Now you your own interactive Tic-Tac-Toe game!

Explanation / Answer

/* * This is a java playing program. * an example of a working program. */ import tio.*; class TicTacToe { static final int EMPTY = 0; static final int NONE = 0; static final int USER = 1; static final int COMPUTER = 2; static final int STALEMATE = 3; public static void main(String[] args) { // Data objects // 1 = user, 2 = computer int turn = USER; // We will represent the board as nine cells // 0 = empty, 1 = user, 2 = computer int[][] board = new int[3][3]; // move: 1-9 representing ul through lr int move; // winner: 0 = none, 1 = user, 2 = computer, 3 = stalemate int winner; // Print Instructions System.out.println("This is a tic-tac-toe game"); System.out.println("You are playing against the computer!"); System.out.println("Enter 1-9 to indicate your move"); // Print the board print_board(board); // While (game not over) while(true) { if(turn == USER) { System.out.println("Your move"); move = -1; while (move9 || board[move/3][move%3] != EMPTY) { System.out.println("Please enter your move(0-9): "); move = Console.in.readInt(); Console.in.readChar(); } } else { move = computer_move(board); System.out.println("Computer move: " + move); } // Update the board board[(int)(move/3)][move%3] = turn; // Print the board print_board(board); // if game is over winner = checkWinner(board); if(winner != NONE) break; // switch turn if(turn == USER) { turn = COMPUTER; } else { turn = USER; } } // Print out the outcome switch(winner) { case USER: System.out.println("You won!"); break; case COMPUTER: System.out.println("Computer won!"); break; default: System.out.println("Tie!"); break; } } // Print the board public static void print_board(int[][] board) { System.out.print(printChar(board[0][0])); System.out.print("|"); System.out.print(printChar(board[0][1])); System.out.print("|"); System.out.println(printChar(board[0][2])); System.out.println("-----"); System.out.print(printChar(board[1][0])); System.out.print("|"); System.out.print(printChar(board[1][1])); System.out.print("|"); System.out.println(printChar(board[1][2])); System.out.println("-----"); System.out.print(printChar(board[2][0])); System.out.print("|"); System.out.print(printChar(board[2][1])); System.out.print("|"); System.out.println(printChar(board[2][2])); } // Return an X or O, depending upon whose move it was public static char printChar(int b) { switch(b) { case EMPTY: return ' '; case USER: return 'X'; case COMPUTER: return 'O'; } return ' '; } // See if the game is over public static int checkWinner(int[][] board) { // Check if someone won // Check horizontals // top row if((board[0][0] == board[0][1]) && (board[0][1] == board[0][2])) return board[0][0]; // middle row if((board[1][0] == board[1][1]) && (board[1][1] == board[1][2])) return board[1][0]; // bottom row if((board[2][0] == board[2][1]) && (board[2][1] == board[2][2])) return board[2][0]; // Check verticals // left column if((board[0][0] == board[1][0]) && (board[1][0] == board[2][0])) return board[0][0]; // middle column if((board[0][1] == board[1][1]) && (board[1][1] == board[2][1])) return board[0][1]; // right column if((board[0][2] == board[1][2]) && (board[1][2] == board[2][2])) return board[0][2]; // Check diagonals // one diagonal if((board[0][0] == board[1][1]) && (board[1][1] == board[2][2])) return board[0][0]; // the other diagonal if((board[0][2] == board[1][1]) && (board[1][1] == board[2][0])) return board[0][2]; // Check if the board is full if(board[0][0] == EMPTY || board[0][1] == EMPTY || board[0][2] == EMPTY || board[1][0] == EMPTY || board[1][1] == EMPTY || board[1][2] == EMPTY || board[2][0] == EMPTY || board[2][1] == EMPTY || board[2][2] == EMPTY) return NONE; return STALEMATE; } // Generate a random computer move public static int computer_move(int[][] board) { int move = (int)(Math.random()*9); while(board[move/3][move%3] != EMPTY) move = (int)(Math.random()*9); return move; } }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote