Question
Hello, Thanks for your time. I need help with writing a tic tac toe game, a certain way. It has the be this way, it
Hello,
Thanks for your time. I need help with writing a tic tac toe game, a certain way. It has the be this way, it cannot be written any other way.
Example 1 -
So, for example this set of input would allow X to win along the main diagonal.
0 0
1 0
1 1
2 0
2 2
Alternatively, the input could be provided in one line:
0 0 1 0 1 1 2 0 2 2
The first pair is for Xs move, second pair for Os move and so on. If you are not having enough input values you will have an error.
Example 2 -
Another possibility is O to win in last column. The input values could be:
1 00 22 00 1 2 1 0 0
This is my the code I have so far. I don't understand how to get it to work if the user inputs his/her data like 01 22 02 12 etc. I am looking for your guidance and help, thank you.
import java.util.Scanner; public class Main { public enum Value { X, O; } private static Value[][] gameBoard = new Value[3][3]; public Main() { } public static void main(String[] x) { Main game = new Main(); Scanner s = new Scanner(System.in); int moves = 0; int row = -1, column = -1; boolean player = false; System.out.print("To enter moves like 0 0 1 0 2 2, enter 3 now: "); String input = ""; while (true) { Value mark = player ? Value.O : Value.X; if (s.nextInt() == 3) { System.out.print("Enter your moves number then space" + " starting with player " + mark + ": "); input = s.nextLine().replaceAll(" ", ""); System.out.println(input); for (int i = 0; i < input.length() - 1; i++) { row = Character.getNumericValue(input.charAt(i)); column = Character.getNumericValue(input.charAt(i + 1)); moves++; if (game.move(row, column, mark)) { game.printBoard(); if (game.validate(row, column, mark)) { System.out.println(mark + " has won. Game over."); break; } else { player = !player; } if (moves == 9) { System.out.println("The game is a draw."); break; } } else { System.out.println("Invalid move (taken or out of bonds). Try again."); } } } else { System.out.print("Enter row for player " + mark + ": "); row = s.nextInt(); System.out.printf("Enter column for player " + mark + ": "); column = s.nextInt(); moves++; if (game.move(row, column, mark)) { game.printBoard(); if (game.validate(row, column, mark)) { System.out.println(mark + " has won. Game over."); break; } else { player = !player; } if (moves == 9) { System.out.println("The game is a draw."); break; } } else { System.out.println("Invalid move (taken or out of bonds). Try again."); } } } s.close(); } public boolean move(int row, int column, Value x) { if (gameBoard[row][column] != Value.X && gameBoard[row][column] != Value.O) { gameBoard[row][column] = x; return true; } return false; } private boolean validate(int row, int col, Value x) { boolean validate1 = true, validate2 = true; if (row == 1 && col == 1) { if ((gameBoard[0][0] == x && gameBoard[2][2] == x) | (gameBoard[0][2] == x && gameBoard[2][0] == x)) return true; } if ((row == 0 | row == 2) && (col == 0 | col == 2)) { if (gameBoard[1][1] == x && gameBoard[2 - row][2 - col] == x) return true; } for (int i = 0; i < 3; i++) { if (gameBoard[row][i] != x) { validate1 = false; break; } } for (int i = 0; i < 3; i++) { if (gameBoard[i][col] != x) { validate2 = false; break; } } return validate1 | validate2; } public void printBoard() { for (Value[] row : gameBoard) { for (Value element : row) { if (element == Value.O || element == Value.X) System.out.print(" " + element + " "); else System.out.print(" | "); } System.out.println(); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started