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

Define high-level thread states?, The state chart diagram given below defin...

The state chart diagram given below defines the thread states.     1.      Runnable - waiting for its turn to be picked for performing by the thread scheduler based o

Write a program using local variable, Write a program using local variable ...

Write a program using local variable Public void someMethod( ) { int x; //local variable System.out.println(x); // compile time error } - Constructor - Doesn't

I need viral script for facebook likes, Project Description: I need a Sc...

Project Description: I need a Script developer who can make script for me. Need urgently. Skills required: Java, Facebook Marketing, Javascript, PHP, Script Install

What are the steps of working of tcp and ip protocols, What are the steps o...

What are the steps of working of TCP and IP protocols within internet transmission? TCP divide messages in several packets (capsulation) and sent these messages over the internet

Zhtml requirements, In addition to your Java code requirements, you also ne...

In addition to your Java code requirements, you also need to enhance the ZHTML for your application. As before, you can continue working with the page(s) you used for the previous

Describe what is meant by a constructor in java, Question: (a) Describe...

Question: (a) Describe what is meant by a ‘constructor' in Java. Use a simple example to illustrate your answer. (b) Differentiate between a class method and an instance me

Queues, we can insert elements at rear and remove at front bt my question i...

we can insert elements at rear and remove at front bt my question is that how we insert at front in circular queue

Why you don''t need to import java.lang.*, Why You don't need to import jav...

Why You don't need to import java.lang.* There is one exception to the import rule. All classes in the java.lang package are imported by default. Therefore you do not required

Calling the function, You may call function from any other place into your ...

You may call function from any other place into your JavaScript code. After the function is executed, the control goes back to other script which called it.  alert('Example 1: t

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