import java.util.Scanner; public clas testing { public static void main(String [
ID: 3534542 • Letter: I
Question
import java.util.Scanner; public clas testing
{ 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 ' '; } }
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.