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

What is significance of jsf- spring integration, Spring - JSF integration i...

Spring - JSF integration is useful when an event handler wishes to explicitly invoke the bean factory to make beans on demand, such as a bean that encapsulates the business logic t

Networking, which concept is used to connect the networking in java

which concept is used to connect the networking in java

What is a jointpoint, A joinpoint is a point in the implementation of the a...

A joinpoint is a point in the implementation of the application where an aspect can be plugged in. This point could be a method being called, an exception being thrown, or even a f

Whatsapp bulk messenger to unlimited numbers, Whatsapp bulk messenger to un...

Whatsapp bulk messenger to unlimited numbers Project Description: Develop a desktop application or script solution to bulk whatsapp sms without getting any block from the wha

How to catch multiple exceptions explain with example, How to catch multipl...

How to catch multiple exceptions explain with example? If multiple blocks match the exception type, the first block in which matches the kinds of the exception catches it. pub

What is inheritance in java explain with example, What is Inheritance in ja...

What is Inheritance in java Explain with example? Code reusability is claimed to be a key advantage of object-oriented languages over non-object-oriented languages. Inheritance

Java error handling mechanism, Errors: When a dynamic linking failure or ...

Errors: When a dynamic linking failure or some other "hard" failure in the virtual machine happens, the virtual machine gives an Error. Typical Java programs could not find Error

Give an examples of access protection, Give an Examples of Access Protectio...

Give an Examples of Access Protection ? This is how the Car class would almost certainly be written in practice. Remember that all the fields are now declared private, and the

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