Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA!!! Very basic, intro java. Nothing advanced. Follow instructions exactly, nothing additional than what is stated. Thanks. You will write a tic-tac-toe game which stores

JAVA!!! Very basic, intro java. Nothing advanced. Follow instructions exactly, nothing additional than what is stated. Thanks.

You will write a tic-tac-toe game which stores the game board as an array of length 9.

Starter Code:

import java.util.Random; import java.util.Scanner; public class TicTacToe { /** * Initializes the board by assigning all array elements the value of '-' * @param board the array representing the tic-tac-toe board */ public static void initializeBoard(char board[]) { } /** * Capitalizes the x or o entered by a player * Ignores already capitalized X and O * @param character the x or o * @return the capitalized X or O */ public static char capitalize(String character) { return '\0'; } /** * Prints the board to the console in the form of a grid, * including column and row numbers. Also prints

* Tic-Tac-Toe Board above the board. For example:

* Tic-Tac-Toe:

* 1 2 3 * 1 O - -

* 2 - X -

* 3 - - -

* @param board the array representing the tic-tac-toe board */ public static void printBoard(char board[]) { System.out.print(" Tic-Tac-Toe: "); } /** * Converts the player choice of row and column * in the form of RC into the correct index of * the board array. * Hint: Use integer division by 10 to extract the row * Hint: Use modulus by 10 to extract the column * @param rowCol the row and column, e.g. 12 or 33 * @return the correct index of the array that * corresponds to the row and column */ public static int convertToIndex(int rowCol) { return -1; } /** * Determines whether a particular position * on the board has already been taken. * @param board the array representing the game board * @param position the position to check * @return whether that position has already been taken */ public static boolean alreadyTaken(char board[], int position) { return false; } /** * Places an X or O into the correct position on the board. * Called when either the player or computer makes its move. * @param board the array representing the tic-tac-toe board * @param position the position in the array at which to place the X or O * @param character either X or O */ public static void makePlacement(char board[], int position, char character) { } /** * Gives a random position on the board * Used for generating moves by the computer * @param size the length of the board array * @return a random index in the board array */ public static int randomPosition(int size) { Random r = new Random(System.currentTimeMillis()); return r.nextInt(size); } /** * Determines whether three characters are all Xs or all Os * Used as a helper method to the gameOver method * @param a the first character to compare, either X, O, or - * @param b the second character to compare, either X, O, or - * @param c the third character to compare, either X, O or - * @return whether the characters are all Xs or all Os */ public static boolean threeInRow(char a, char b, char c) { return false; } /** * Determines whether the game is over * due to one player winning, using * a series of if statements. * Calls the threeInRow method for each * possible row on the board. * @param board the tic-tac-toe game board * @return whether the game is over */ public static boolean gameOverWinner(char board[]) { return false; } public static void main(String[] args) { System.out.println("Welcome to Tic-Tac-Toe!"); char board[] = new char[9]; int numMoves = 0; //increments when player or game A.I. makes a move } }

Sample Output:

Welcome to Tic-Tac-Toe! Would you like to play as X or O: x Tic-Tac-Toe: 1 2 3 1 - - - 2 - - - 3 - - - Please enter your move: 22 Tic-Tac-Toe: 1 2 3 1 - - - 2 - X - 3 - - - Counter move! Tic-Tac-Toe: 1 2 3 1 - - - 2 - X - 3 - O - Please enter your move: 11 Tic-Tac-Toe: 1 2 3 1 X - - 2 - X - 3 - O - Counter move! Tic-Tac-Toe: 1 2 3 1 X O - 2 - X - 3 - O - Please enter your move: 33 Tic-Tac-Toe: 1 2 3 1 X O - 2 - X - 3 - O X X wins! ***Game Over***

Alternately:

Welcome to Tic-Tac-Toe! Would you like to play as X or O: o Tic-Tac-Toe: 1 2 3 1 - - - 2 - - - 3 - - - Please enter your move: 33 Tic-Tac-Toe: 1 2 3 1 - - - 2 - - - 3 - - O Counter move! Tic-Tac-Toe: 1 2 3 1 - - - 2 - X - 3 - - O Please enter your move: 13 Tic-Tac-Toe: 1 2 3 1 - - O 2 - X - 3 - - O Counter move! Tic-Tac-Toe: 1 2 3 1 - - O 2 X X - 3 - - O Please enter your move: 23 Tic-Tac-Toe: 1 2 3 1 - - O 2 X X O 3 - - O

O wins!

***Game Over***

Alternately (spot already taken):

Welcome to Tic-Tac-Toe! Would you like to play as X or O: X Tic-Tac-Toe: 1 2 3 1 - - - 2 - - - 3 - - - Please enter your move: 11 Tic-Tac-Toe: 1 2 3 1 X - - 2 - - - 3 - - - Counter move! Tic-Tac-Toe: 1 2 3 1 X - - 2 - O - 3 - - - Please enter your move: 22 That spot is already taken! Please enter your move: 31 Tic-Tac-Toe: 1 2 3 1 X - - 2 - O - 3 X - - Counter move! Tic-Tac-Toe: 1 2 3 1 X O - 2 - O - 3 X - - Please enter your move: 33 Tic-Tac-Toe: 1 2 3 1 X O - 2 - O - 3 X - X Counter move! Tic-Tac-Toe: 1 2 3 1 X O - 2 O O - 3 X - X Please enter your move: 23 Tic-Tac-Toe: 1 2 3 1 X O - 2 O O X 3 X - X Counter move! Tic-Tac-Toe: 1 2 3 1 X O - 2 O O X 3 X O X O wins! ***Game Over***

Alternately (Tie Game):

Welcome to Tic-Tac-Toe! Would you like to play as X or O: O Tic-Tac-Toe: 1 2 3 1 - - - 2 - - - 3 - - - Please enter your move: 33 Tic-Tac-Toe: 1 2 3 1 - - - 2 - - - 3 - - O Counter move! Tic-Tac-Toe: 1 2 3 1 - X - 2 - - - 3 - - O Please enter your move: 21 Tic-Tac-Toe: 1 2 3 1 - X - 2 O - - 3 - - O Counter move! Tic-Tac-Toe: 1 2 3 1 - X - 2 O X - 3 - - O Please enter your move: 32 Tic-Tac-Toe: 1 2 3 1 - X - 2 O X - 3 - O O Counter move! Tic-Tac-Toe: 1 2 3 1 - X - 2 O X X 3 - O O Please enter your move: 13 Tic-Tac-Toe: 1 2 3 1 - X O 2 O X X 3 - O O Counter move! Tic-Tac-Toe: 1 2 3 1 - X O 2 O X X 3 X O O Please enter your move: 11 Tic-Tac-Toe: 1 2 3 1 O X O 2 O X X 3 X O O It's a tie! ***Game Over***

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions