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

Java Gui, Does the java applet using swing components, JAVA Programming on ...

Does the java applet using swing components, JAVA Programming on this contain all the source codes for download?

Object and Instance, What is the difference between instance and object of ...

What is the difference between instance and object of a class? Few says both are same, then why java kept both the words for same thing?

Multidimensional Array and the ArrayList Class, Write a ComputerPart class ...

Write a ComputerPart class and a ComputerKit class and a client class to test them. The ComputerPart class has two variables: a String representing an item, and a double representi

GUI, How can I connect my java application with xampp

How can I connect my java application with xampp

Develop a supply chain management, Develop a Supply Chain Management Pro...

Develop a Supply Chain Management Project Description: Supply Chain Management from HK and China Skills required is Java

What are the usages of package in java? , It helps to resolve naming confli...

It helps to resolve naming conflicts when different packages have classes with the same names. This also helps you prepare files within your project. As define : java.io package d

Web application using javascript-xml and css, Journal of how you came to AU...

Journal of how you came to AUB Write a web application using JavaScript, XML and CSS that contains a journal of major events and places on your journey to Bond University. The

Explain arithmetic promotion and binary operations, Explain Arithmetic Prom...

Explain Arithmetic Promotion and Binary Operations? An int divided through an int is an int, and a double divided by a double is a double, but what about an int divided by a do

How do i include static files within a jsp page, Static resources should al...

Static resources should always be contained using the JSP include directive. This way, the inclusion is performed just once during the translation phase. Do note that you should al

String, in java string is a

in java string is a

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