import java.util.Scanner; public class Prog08 { public static void main(String [
ID: 3534924 • Letter: I
Question
import java.util.Scanner; public class Prog08 { public static void main(String [] args) { Scanner stdIn = new Scanner(System.in); System.out.println(" Welcome to Tic-Tac-Toe!"); String board = "| | | | | | | | | | | | "; int move; char symbol = 'X'; int moves = 0; System.out.println("---------------------------------------"); System.out.println("To start the game, X goes first. "); do { //get move move = getMove(stdIn, board); ++moves; // record move on board board = recordMove(board, move, symbol); //check for winner if (isWinner(board) != ' ') { System.out.println("---------------------------------------"); System.out.println("Thank you, " + symbol + " has won!"); System.out.println(board); System.out.println(); System.out.println("---------------------------------------"); System.out.println("---------------------------------------"); System.out.println(); return; } // change player else { if (symbol == 'X') symbol = 'O'; else symbol = 'X'; // check for stalemate if (moves < 9) { System.out.println("---------------------------------------"); System.out.println("Thank you, it is now " + symbol + "'s turn."); } } } while (moves < 9); // report for stalemate System.out.println("---------------------------------------"); System.out.println("Thank you, the game is a Stalemate!"); System.out.println(board); System.out.println(); System.out.println("---------------------------------------"); System.out.println("---------------------------------------"); System.out.println(); stdIn.close(); } public static int getMove(Scanner stdIn, String board) { int move; System.out.println(board); do { System.out.print("Please enter a square (1-9): "); move = stdIn.nextInt(); } while ((move < 1 || move > 9) || !isOpenMove(board, move)); return move; } public static boolean isOpenMove(String board, int location) { if (location < 1 || location > 9) return false; if (location == 1 && board.charAt(1) == ' ') return true; if (location == 2 && board.charAt(3) == ' ') return true; if (location == 3 && board.charAt(5) == ' ') return true; if (location == 4 && board.charAt(9) == ' ') return true; if (location == 5 && board.charAt(11) == ' ') return true; if (location == 6 && board.charAt(13) == ' ') return true; if (location == 7 && board.charAt(17) == ' ') return true; if (location == 8 && board.charAt(19) == ' ') return true; if (location == 9 && board.charAt(21) == ' ') return true; return false; } public static String recordMove(String board, int move, char symbol) { if (move == 1) board = board.substring(0, 1) + symbol + board.substring(2); else if (move == 2) board = board.substring(0, 3) + symbol + board.substring(4); else if (move == 3) board = board.substring(0, 5) + symbol + board.substring(6); else if (move == 4) board = board.substring(0, 9) + symbol + board.substring(10); else if (move == 5) board = board.substring(0, 11) + symbol + board.substring(12); else if (move == 6) board = board.substring(0, 13) + symbol + board.substring(14); else if (move == 7) board = board.substring(0, 17) + symbol + board.substring(18); else if (move == 8) board = board.substring(0, 19) + symbol + board.substring(20); else if (move == 9) board = board.substring(0, 21) + symbol + board.substring(22); return board; } public static char checkThree(String board, int pos1, int pos2, int pos3) { if (board.charAt(pos1) == board.charAt(pos2) && board.charAt(pos2) == board.charAt(pos3) && board.charAt(pos1) != ' ') return board.charAt(pos1); else return ' '; } public static char isWinner(String board) { char c; // rows c = checkThree(board, 1, 3, 5); if (c != ' ') return c; c = checkThree(board, 9, 11, 13); if (c != ' ') return c; c = checkThree(board, 17, 19, 21); if (c != ' ') return c; // cols c = checkThree(board, 1, 9, 17); if (c != ' ') return c; c = checkThree(board, 3, 11, 19); if (c != ' ') return c; c = checkThree(board, 5, 13, 21); if (c != ' ') return c; // dias c = checkThree(board, 1, 11, 21); if (c != ' ') return c; c = checkThree(board, 5, 11, 17); if (c != ' ') return c; return ' '; } }
Main topics: Programmer Defined Methods
Arrays
Program Specification:
Write a program that allows two individuals to play a single game of Tic-Tac-Toe.
Rules and Requirements:
You are to re-write Program 08, using a char array to represent the board instead of a string.
You must use a the following char Array to represent the nine squares of the board. Notice: board[0] will not be used.
char [] board = new char[10];
You must re-write (and use) all of the methods indicated in Program 08, those given (by definition) as well as those prescribed (by heading), to take and use the array board (instead of the String board).
Your program may use additional methods where appropriate.
You must write a main method that allows a pair of users to play a game of Tic-Tac-Toe such that:
Each turn of the game:
It must be clear whose turn it is X's or O's.
The current board configuration must be displayed in a reasonable format.
An available square number [1, 9] is obtained from a player and is used make their (valid) move on the board.
The game ends when there is a Tic-Tac-Toe (win) or all the positions of the board are full (stalemate). A simple report indicates which has occurred and in the case of a win, who has won X or O.
Notes and Hints:
Use the board's [] operator to access the nine individual char locations in the board array:
Each of the nine char locations with in the board string should at all times hold exactly one of the following char literals:
' ', indicating the square is available.
'X', indicating the square has an X in it.
'O', indicating the square has an O in it.
The elements of a char array are NOT initialized to ' ', you may want to write a separated method to do this.
You will NOT be able to just System.out.println(board): as in Program 08, you may want to write a separate method to do this.
Use a char variable to keep track of who's turn it is: X's or O's.
Use a int variable to keep track of how many turns have passed in the game (nine at most).
Just let X go first.
Your main from Program 08 will need very little modification if you have followed the above rules, notes and hints.
Sample run(s):
Welcome to Tic-Tac-Toe!
---------------------------------------
To start the game, X goes first.
| | | |
| | | |
| | | |
Please enter a square (1-9): 5
---------------------------------------
Thank you, it is now O's turn.
| | | |
| |X| |
| | | |
Please enter a square (1-9): 2
---------------------------------------
Thank you, it is now X's turn.
| |O| |
| |X| |
| | | |
Please enter a square (1-9): 1
---------------------------------------
Thank you, it is now O's turn.
|X|O| |
| |X| |
| | | |
Please enter a square (1-9): 9
---------------------------------------
Thank you, it is now X's turn.
|X|O| |
| |X| |
| | |O|
Please enter a square (1-9): 4
---------------------------------------
Thank you, it is now O's turn.
|X|O| |
|X|X| |
| | |O|
Please enter a square (1-9): 6
---------------------------------------
Thank you, it is now X's turn.
|X|O| |
|X|X|O|
| | |O|
Please enter a square (1-9): 7
---------------------------------------
Thank you, X has won!
|X|O| |
|X|X|O|
|X| |O|
---------------------------------------
---------------------------------------
Welcome to Tic-Tac-Toe!
---------------------------------------
To start the game, X goes first.
| | | |
| | | |
| | | |
Please enter a square (1-9): 1
---------------------------------------
Thank you, it is now O's turn.
|X| | |
| | | |
| | | |
Please enter a square (1-9): 5
---------------------------------------
Thank you, it is now X's turn.
|X| | |
| |O| |
| | | |
Please enter a square (1-9): 4
---------------------------------------
Thank you, it is now O's turn.
|X| | |
|X|O| |
| | | |
Please enter a square (1-9): 7
---------------------------------------
Thank you, it is now X's turn.
|X| | |
|X|O| |
|O| | |
Please enter a square (1-9): 3
---------------------------------------
Thank you, it is now O's turn.
|X| |X|
|X|O| |
|O| | |
Please enter a square (1-9): 2
---------------------------------------
Thank you, it is now X's turn.
|X|O|X|
|X|O| |
|O| | |
Please enter a square (1-9): 8
---------------------------------------
Thank you, it is now O's turn.
|X|O|X|
|X|O| |
|O|X| |
Please enter a square (1-9): 9
---------------------------------------
Thank you, it is now X's turn.
|X|O|X|
|X|O| |
|O|X|O|
Please enter a square (1-9): 6
---------------------------------------
Thank you, the game is a Stalemate!
|X|O|X|
|X|O|X|
|O|X|O|
---------------------------------------
---------------------------------------
Explanation / Answer
Please Rate :
import java.util.Scanner;
public class TicTacToe
{
public static void showBoard(char[] a)
{
for(int i=1;i<10;i++)
{
System.out.print("|"+a[i]);
if(i==3 || i==6 || i==9)
{
System.out.println("|");
System.out.println();
}
}
}
public static void main(String [] args)
{
Scanner stdIn = new Scanner(System.in);
System.out.println(" Welcome to Tic-Tac-Toe!");
char[] board= new char[10];
int move;
char symbol = 'X';
int moves = 0;
System.out.println("---------------------------------------");
System.out.println("To start the game, X goes first. ");
do
{
//get move
move = getMove(stdIn, board);
++moves;
// record move on board
board = recordMove(board, move, symbol);
//check for winner
if (isWinner(board) != ' ')
{
System.out.println("---------------------------------------");
System.out.println("Thank you, " + symbol + " has won!");
showBoard(board);
System.out.println();
System.out.println("---------------------------------------");
System.out.println("---------------------------------------");
System.out.println();
return;
}
// change player
else
{
if (symbol == 'X')
symbol = 'O';
else
symbol = 'X';
// check for stalemate
if (moves < 9)
{
System.out.println("---------------------------------------");
System.out.println("Thank you, it is now " + symbol + "'s turn.");
}
}
} while (moves < 9);
// report for stalemate
System.out.println("---------------------------------------");
System.out.println("Thank you, the game is a Stalemate!");
showBoard(board);
System.out.println();
System.out.println("---------------------------------------");
System.out.println("---------------------------------------");
System.out.println();
stdIn.close();
}
public static int getMove(Scanner stdIn, char[] board)
{
int move;
showBoard(board);
do
{
System.out.print("Please enter a square (1-9): ");
move = stdIn.nextInt();
} while ((move < 1 || move > 9) || !isOpenMove(board, move));
return move;
}
public static boolean isOpenMove(char[] board, int location)
{
if (location < 1 || location > 9)
return false;
if (location == 1 && board[1] == '')
return true;
if (location == 2 && board[2] == '')
return true;
if (location == 3 && board[3] == '')
return true;
if (location == 4 && board[4] == '')
return true;
if (location == 5 && board[5] == '')
return true;
if (location == 6 && board[6] == '')
return true;
if (location == 7 && board[7] == '')
return true;
if (location == 8 && board[8] == '')
return true;
if (location == 9 && board[9] == '')
return true;
return false;
}
public static char[] recordMove(char[] b, int move, char symbol)
{
String board=new String(b);
if (move == 1)
board = board.substring(0, 1) + symbol + board.substring(2);
else if (move == 2)
board = board.substring(0, 2) + symbol + board.substring(3);
else if (move == 3)
board = board.substring(0, 3) + symbol + board.substring(4);
else if (move == 4)
board = board.substring(0, 4) + symbol + board.substring(5);
else if (move == 5)
board = board.substring(0, 5) + symbol + board.substring(6);
else if (move == 6)
board = board.substring(0, 6) + symbol + board.substring(7);
else if (move == 7)
board = board.substring(0, 7) + symbol + board.substring(8);
else if (move == 8)
board = board.substring(0, 8) + symbol + board.substring(9);
else if (move == 9)
board = board.substring(0, 9) + symbol + board.substring(10);
return board.toCharArray();
}
public static char checkThree(char[] board, int pos1, int pos2, int pos3)
{
if (board[pos1] == board[pos2] &&
board[pos2] == board[pos3] &&
board[pos1] != '')
return board[pos1];
else
return ' ';
}
public static char isWinner(char[] board)
{
char c;
// rows
c = checkThree(board, 1, 2, 3);
if (c != ' ')
return c;
c = checkThree(board, 4, 5, 6);
if (c != ' ')
return c;
c = checkThree(board, 7, 8, 9);
if (c != ' ')
return c;
// cols
c = checkThree(board, 1, 4, 7);
if (c != ' ')
return c;
c = checkThree(board, 2, 5, 8);
if (c != ' ')
return c;
c = checkThree(board, 3, 6, 9);
if (c != ' ')
return c;
// dias
c = checkThree(board, 1, 5, 9);
if (c != ' ')
return c;
c = checkThree(board, 3, 5, 7);
if (c != ' ')
return c;
return ' ';
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.