The tic-tac-toe program, JAVA Programming

Assignment Help:

Instructions

Modify the program so that the computer will generate moves for the
‘O' player (it must continue to accept user input for ‘X'). A simple way to do this is to use a random value between 0 and 9 to generate moves for ‘O'.

currMove = (int)(Math.random() * 9) + 1;

You will have to ensure, of course, that the computer does not generate a move for a square that is already occupied. Another simple way to generate moves is to simply move to the first unoccupied square.

Also I need explain in words (as clearly and precisely as possible) any improvements you made to the program.

Any other improvement to make this program looks better please sent me in a different attach.

The Program

/**
* Homework Assignment #1 -- Working With Two-Dimensional Arrays (Tic-Tac-Toe)
* CPS , Section [[Put your section number here]]
*
* @author [[Put your name here]]
* @version 1.1 [[Put current date here]]
*/
import java.util.Scanner;

public class Hmwk1 {

public static final char SPACE = ' ';
// Tic-tac-toe board showing move numbers
public static final char[][] MOVE_INFO = {{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'}};

public static void main(String[] args) {
Scanner in = new Scanner(System.in);

char[][] board = new char[3][3];
char currPlayer = 'X';

fill2D(board, SPACE);
printBoard(MOVE_INFO);
while (!gameOver(board)) {
System.err.print("Move for " + currPlayer + "? ");
int currMove = in.nextInt();
makeMove(currPlayer, currMove, board);
printBoard(board);
currPlayer = nextPlayer(currPlayer);
}
reportResults(board);
}

/**
* Fill a given two-dimensional character array with a given
* character
*
* @param a the array to fill
* @param c the character to use
*/
public static void fill2D(char[][] a, char c) {
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
a[i][j] = c;
}
}
}

public static final String VERT = " * * ";
public static final String HORZ = "***********";

/**
* Print the contents of an array representing a tic-tac-toe
* board
*
* @param board the board configuration to be printed
*/
public static void printBoard(char[][] board) {
System.out.println();
for (int i = 0; i < board.length; i++) {
System.out.println(VERT);
for (int j = 0; j < board[i].length; j++) {
System.out.print(" " + board[i][j] + " ");
if (j < board[i].length - 1) {
System.out.print("*");
} else {
System.out.println();
}
}
System.out.println(VERT);
if (i < board.length - 1) {
System.out.println(HORZ);
}
}
System.out.println();
}

/**
* Determine if the game is over; i.e., if a player has won or if
* there are no more moves possible
*
* @param board current board configuration
* @return true if game is over, false otherwise
*/
public static boolean gameOver(char[][] board) {
return noMovesLeft(board)
|| isWinner('X', board)
|| isWinner('O', board);
}

/**
* Determine if no more moves are possible; i.e., if there
* are no more SPACE's left in the board array
*
* @param board the current board configuration
* @return true if there are no more legal moves, false otherwise
*/
public static boolean noMovesLeft(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (board[i][j] == SPACE) {
return false;
}
}
}
return true;
}

/**
* Determine if a given player is a winner; i.e., has 3 marks in a
* row, either horizontally, vertically, or diagonally
*
* @param player the player to check
* @param board the current board configuration
* @return true if player has 3 marks in a row, false otherwise
*/
public static boolean isWinner(char player, char[][] board) {
// check rows (horizontal)
for (int row = 0; row < 3; row++) {
if (board[row][0] == player && board[row][1] == player
&& board[row][2] == player) {
return true;
}
}
// check cols (vertical)
for (int col = 0; col < 3; col++) {
if (board[0][col] == player && board[1][col] == player
&& board[2][col] == player) {
return true;
}
}
// check diagonals
if (board[0][0] == player && board[1][1] == player
&& board[2][2] == player) {
return true;
} else if (board[0][2] == player && board[1][1] == player
&& board[2][0] == player) {
return true;
} else {
return false;
}
}

/**
* Mark the specified move for the specified player on the given
* board configuration (move == 1 => [0][0], move == 2 => [0][1],
* move == 3 => [0][2], move == 4 => [1][0], move == 5 => [1][1],
* move == 6 => [1][2], move == 7 => [2][0], move == 8 => [2][1],
* move == 9 => [2][2])
*
* @param player the player making the move
* @param move the move to be made
* @param board the current board configuration
*/
public static void makeMove(char player, int move, char[][] board) {
int row = (move - 1) / board.length;
int col = (move - 1) % board[row].length;
board[row][col] = player;
}

/**
* Determine the next player, for cycling X, then O, then X, etc.
*
* @param current the current player ('X' or 'O')
* @return the next player to play ('O' or 'X')
*/
public static char nextPlayer(char current) {
if (current == 'X') {
return 'O';
} else {
return 'X';
}
}

/**
* Report the results of the game, whether X or O won or the game
* ended in a tie.
*
* @param board the board configuration to be described
*/
public static void reportResults(char[][] board) {
if (isWinner('O', board)) {
System.out.println("O is the winner!");
} else if (isWinner('X', board)) {
System.out.println("X is the winner!");
} else {
System.out.println("The game ends in a tie.");
}
}

}


Related Discussions:- The tic-tac-toe program

Website for swapping and selling books, Website for swapping/selling books ...

Website for swapping/selling books Project Description: Seeking a website where students can sell and buy books. I want them to swap books as well. Payment options are requir

Which is better to store data as attributes or as elements? , A question ri...

A question rising in the mind of XML/DTD designers is whether to model and encode retain information using an attribute, or alternatively, using an element. 1. Using  an  elemen

What is aop module, The AOP module is used for developing aspects for our S...

The AOP module is used for developing aspects for our Spring-enabled application. Much of the support has been given by the AOP Alliance in order to ensure the interoperability amo

Write html and javascript code for the needs, Write HTML and JavaScript cod...

Write HTML and JavaScript code for the needs shown in the diagram below? When ConvertoUpperCase button is pressed then the entered text is converted to upper case and result is

Create an online multiple choice quiz, Create an online multiple-choice qui...

Create an online multiple-choice quiz using JSP/Servlets technology. The quiz should draw questions from an array of predefined questions (at least fifteen). You should use a meani

Area under curve., write a program to find the area under the curve y=f(x) ...

write a program to find the area under the curve y=f(x) between x=a and x=b,integrate y=f(x) between the limits of a and b.   #include float start_point,

Java remote method invocation (RMI) architecture, Java Remote Method Invoca...

Java Remote Method Invocation (RMI): It gives a way for a Java program on one machine to interact with objects residing in different JVMs. The important blocks of the RMI arc

String, solve the string public int expression("10+20+30")

solve the string public int expression("10+20+30")

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd