Question: Java You will implement a procedural program (i.e. not object- oriented) using static methods of Java. This program will randomly play a game of Tic-Tac-Toe

Java

You will implement a procedural program (i.e. not object- oriented) using static methods of Java. This program will randomly play a game of Tic-Tac-Toe without winning or losing. The program will print the game board after each play. The game will always have 9 plays, where player X will first randomly place X in a cell which is followed by player O and then they take turns until the board is filled.

We represent the 3 by 3 game board using an array of 9 integers. The array initially is filled with 0. (You dont have to explicitly initialize the array content since by default int array contains 0.) We use 1 to represent X and 2 to represent O. The array is stored in a static field grid. We store the cells in row 1 first, followed by cells in row 2, and then cells in row 3. There is also a static field player that represents the current player (with value that is either 1 or 2).

You should implement 5 methods. The requirement of each method is in the comments above the method.

Output

play 1:

| | X

-----------

| |

-----------

| |

play 2:

| | X

-----------

| |

-----------

| | O

play 3:

| | X

-----------

| |

-----------

X| | O

play 4:

| | X

-----------

| |

-----------

X | O | O

play 5:

| | X

-----------

X | |

-----------

X | O | O

play 6:

O | | X

-----------

X| |

-----------

X | O | O

play 7:

O | | X

-----------

X | X |

-----------

X | O | O

play 8:

O | O | X

-----------

X | X |

-----------

X | O | O

play 9:

O | O | X

-----------

X | X | X

-----------

X | O | O

Note that since the play is random, each time you run your program, you should expect different output.

_______________________________________

Code Provided

import java.util.Random;

public class ttt { public static void main(String[] args) { // play up to 9 times for(int i=0; i<9; i++) { play(); System.out.println(" play " + (i+1) + ": "); print(); } } // The static field "grid" represents a 3 by 3 Tic-Tac-Toe grid as an array of 9 integers, // which stores the cells of row 1, row 2, and row 3 consecutively in that order. // For each array element, 0 represents empty cell, 1 represents player X, 2 represents player O /* * O | | X * ----------- * X | X | * ----------- * X | O | O * * is represented by the array {2, 0, 1, 1, 1, 0, 1, 2, 2} */ static int[] grid = new int[9]; // The static field "player" represents the current player (1 or 2). The initial player is X. static int player = 1; // Change the static field "player" from either 1 to 2 or from 2 to 1 static private void flip() { // TODO }

// 1. Randomly pick an empty cell and fill it with the integer representing the current player // That is, fill the empty cell with 1 if current player is X and fill 2 otherwise // 2. Flip the current player static void play() { // TODO }

// generate a random index between 0 to 8 static int nextPlay() { // TODO }

// Generate the string representation of a cell // 1. return a string 3 empty spaces if the cell is blank // 2. return " X " if "grid" at index "pos" is 1 // 3. return " O " if "grid" at index "pos" 2 static private String getCell(int pos) { // TODO }

// Print the array "grid" as game board. For example, /* * If "grid" is {2, 0, 1, 1, 1, 0, 1, 2, 2}, you print * * O | | X * ----------- * X | X | * ----------- * X | O | O * */ static void print() { // TODO } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!