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

How to execute class with a main() method in a package, Example:  You have...

Example:  You have a class named "Set" in a project folder "e:\myProject" and package named com.zxy.client, will you be able to compile and execute it as it is

I want java web developer finish a webcam site, Java Web Developer A web...

Java Web Developer A web developer is required to finish a webcam site with basic account management and voting functionality. The website can make use of a third party flash

Differentiation between an applet and an application, Differentiation betwe...

Differentiation between an Applet and an Application ?

Explain identifiers in java, Explain Identifiers in Java ? Identifiers ...

Explain Identifiers in Java ? Identifiers are the names of variables, classes, methods, packages and interfaces. Unlike literals they are not the things themselves, only ways o

Toward privacy preserving and collusion resistance, Need Toward Privacy Pre...

Need Toward Privacy Preserving and Collusion Resistance in a Location Proof Updating System Project Description: Today's location-sensitive service relies on user's mobile de

Difference between an interface and an abstract class, What is the differen...

What is the difference between an Interface and an Abstract class? An abstract class can have instance methods that execute a default behavior. An Interface can only declare co

Explain the three benefits of access protection, Explain the Three Benefits...

Explain the Three Benefits of Access Protection ? Access protection has three major benefits: 1. It permits you to enforce constraints on an object's state. 2. It gives a si

Describe exception classes, Java Programming 1. Discuss any five buzz w...

Java Programming 1. Discuss any five buzz words in Java. 2. Describe exception classes and also explain common exceptions in java. 3. Describe primitive and abstract data

What is an abstract class, What is an abstract class? Abstract class mu...

What is an abstract class? Abstract class must be extended/subclassed (to be useful). It serves as a template. A class that is abstract may not be instantiated (ie. you may not

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