How can I keep it running after checking the if condition? - java

This is a program tic tac toe. It's a computer operation. I want it to check all conditions. If it meets the condition, it will check again if the position of the computer to work. Can add O or not if it can't add O. had the computer check the next condition completely, if it didn't meet the condition it would randomly add O, but now it doesn't work because it happens that sometimes the computer randomly added it in the first place. and then when the players Played the condition that the computer had to prevent the player from winning, but it had a random O In the previous round, it was unable to fill the O at all, thus preventing it from continuing.
private static void computerNormalTurn(char[][] board) {
Random rand = new Random();
int computerMove;
while (true) {
if(board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X'){
if(board[2][2] != ' ') {
continue;
}else{
computerMove = 3;
}
} else if (board [2][0] == 'X' && board [2][2] == 'X' ||
board [1][1] == 'X' && board [0][1] == 'X') {
if(board[2][1] != ' ') {
continue;
}else{
computerMove = 2;
}
} else if (board [2][1] == 'X' && board [2][2] == 'X' ||
board [1][0] == 'X' && board [0][0] == 'X' ||
board [1][1] == 'X' && board [0][2] == 'X' ) {
if(board[2][0] != ' ') {
continue;
}else{
computerMove = 1;
}
} else if (board[2][0] == 'X' && board[0][0] == 'X' ||
board [1][1] == 'X' && board [1][2] == 'X' ) {
if(board[1][0] != ' ') {
continue;
}else{
computerMove = 4;
}
} else if (board [2][0] == 'X' && board [0][2] == 'X' ||
board [2][1] == 'X' && board [0][1] == 'X' ||
board [2][2] == 'X' && board [0][0] == 'X' ||
board [1][0] == 'X' && board [1][2] == 'X' ) {
if(board[1][1] != ' ') {
continue;
}else{
computerMove = 5;
}
} else if (board[2][2] == 'X' && board[0][2] == 'X' ||
board [1][0] == 'X' && board [1][1] == 'X') {
if(board[1][2] != ' ') {
continue;
}else{
computerMove = 6;
}
} else if (board[2][0] == 'X' && board[1][0] == 'X' ||
board [2][2] == 'X' && board [1][1] == 'X' ||
board [0][1] == 'X' && board [0][2] == 'X') {
if(board[0][0] != ' ') {
continue;
}else{
computerMove = 7;
}
} else if (board [2][1] == 'X' && board [1][1] == 'X' ||
board [0][0] == 'X' && board [0][2] == 'X' ) {
if(board[0][1] != ' ') {
continue;
}else{
computerMove = 8;
}
} else if (board[2][0] == 'X' && board[1][1] == 'X' ||
board [2][2] == 'X' && board [1][2] == 'X' ||
board [0][0] == 'X' && board [0][1] == 'X' ) {
if(board[0][2] != ' ') {
continue;
}else{
computerMove = 9;
}
}else {
computerMove = rand.nextInt(9) + 1;
}
if (isValidMove(board, Integer.toString(computerMove))) {
break;
}
}
placeMove(board, Integer.toString(computerMove), 'O');
System.out.println("Computer chose " + computerMove);
}

I think the problem is that if your board is empty the computer will still have a move because of the else condition else { computerMove = rand.nextInt(9)+1; }
Then the following if statement will give a valid move and you will break the while loop and place the move on the board.
So if you want to prevent the computer from playing first, you should make a function that checks if the board is empty and if it is then do nothing.
public boolean isEmpty(){
for(int i = 0; i<3; i++)
for(int j = 0; j<3; j++)
if(board[i][j] != ' ') return false;
return true
}
If needed you can pass the board as a parameter in the function.
Hope I helped. Good luck!

There's a problem with the continues. The continue causes the next iteration. Because nothing changed, the game stays in the same state, the same condition will be met and the loop will continue endless.
Rewrite your conditions, so instead of e.g.
if(board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X'){
if(board[2][2] != ' ') {
continue;
}else{
computerMove = 3;
}
} else ...
write
if((board [2][0] == 'X' && board [2][1] == 'X' ||
board [1][1] == 'X' && board [0][0] == 'X' ||
board [1][2] == 'X' && board [0][2] == 'X') &&
board [2][2] == ' '){
computerMove = 3;
} else ...

Related

Sorting through 2D array for Tic Tac Toe winner

I have made this very obnoxious way of finding the winner in a tic tac toe game and I was just wondering if there is a simpler way of doing this.
This is the method I currently have, and as you can see it is very redundant and repetitive. Any tips to narrow this down would be amazing. I'm thinking maybe a nested for-loop might work but not entirely sure how to set that up within this.
public static boolean isGameOver(char[][] gameBoard) {
//Testing for Horizontal Win
if(gameBoard[0][0] == 'X' && gameBoard[0][2] == 'X' && gameBoard [0][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][0] == 'O' && gameBoard[0][2] == 'O' && gameBoard [0][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[2][0] == 'X' && gameBoard[2][2] == 'X' && gameBoard [2][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[2][0] == 'O' && gameBoard[2][2] == 'O' && gameBoard [2][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[4][0] == 'X' && gameBoard[4][2] == 'X' && gameBoard [4][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[4][0] == 'O' && gameBoard[4][2] == 'O' && gameBoard [4][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
//Testing for Vertical Win
if(gameBoard[0][0] == 'X' && gameBoard[2][0] == 'X' && gameBoard [4][0] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][0] == 'O' && gameBoard[2][0] == 'O' && gameBoard [4][0] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[0][2] == 'X' && gameBoard[2][2] == 'X' && gameBoard [4][2] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][2] == 'O' && gameBoard[2][2] == 'O' && gameBoard [4][2] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[0][4] == 'X' && gameBoard[2][4] == 'X' && gameBoard [4][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][4] == 'O' && gameBoard[2][4] == 'O' && gameBoard [4][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
//Testing for Diagonal Win
if(gameBoard[0][0] == 'X' && gameBoard[2][2] == 'X' && gameBoard [4][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[0][0] == 'O' && gameBoard[2][2] == 'O' && gameBoard [4][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
if(gameBoard[4][0] == 'X' && gameBoard[2][2] == 'X' && gameBoard [0][4] == 'X') {
System.out.println("Player Wins!\n");
playerScore++;
return true;
}
if(gameBoard[4][0] == 'O' && gameBoard[2][2] == 'O' && gameBoard [0][4] == 'O') {
System.out.println("CPU Wins!\n");
cpuScore++;
return true;
}
//Testing for Tie
if(gameBoard[0][0] != ' ' && gameBoard[0][2] != ' ' && gameBoard[0][4] != ' ' &&
gameBoard[2][0] != ' ' && gameBoard[2][2] != ' ' && gameBoard[2][4] != ' ' &&
gameBoard[4][0] != ' ' && gameBoard[4][2] != ' ' && gameBoard[4][4] != ' ') {
System.out.println("It's a tie!!!\n");
numOfTies++;
return true;
}
return false;
}
Ok, I got a little carried away. But perhaps you could use some ideas presented here. My main goal was to make it so the entire board need not be checked after each move. This is the type of issue that is best considered during the design phase of a program.
I created a TriGroup class (which is essentially a mutable string to hold successive moves.
Then a map is used to hold all the groupings which have a common coordinate.
when a move is made, those groupings have the current player appended.
and check is done to see if that player won.
this program will run by itself using random moves resulting in a victory or a tie.
Some border cases may have been overlooked.
public class TicTacToeCheck {
int moveCount = 0;
static int MAX_MOVES = 27;
class TriGroup {
public String group = "";
#Override
public String toString() {
return group;
}
}
TriGroup row1 = new TriGroup();
TriGroup row2 = new TriGroup();
TriGroup row3 = new TriGroup();
TriGroup col1 = new TriGroup();
TriGroup col2 = new TriGroup();
TriGroup col3 = new TriGroup();
TriGroup diag1 = new TriGroup();
TriGroup diag2 = new TriGroup();
Map<String, List<TriGroup>> commonGroupings = new HashMap<>();
{
commonGroupings.put("00", List.of(row1, col1, diag1));
commonGroupings.put("02", List.of(row1, col2));
commonGroupings.put("04", List.of(row1, col3));
commonGroupings.put("20", List.of(row2, col1));
commonGroupings.put("22", List.of(row2, col2, diag1, diag2));
commonGroupings.put("24", List.of(row2, col3));
commonGroupings.put("40", List.of(row3, col1, diag1));
commonGroupings.put("42", List.of(row3, col2));
commonGroupings.put("44", List.of(row3, col3));
}
public static void main(String[] args) {
new TicTacToeCheck().start();
}
public void start() {
char player = 'X';
Random r = new Random();
outer: while (moveCount < MAX_MOVES) {
commonGroupings.entrySet().forEach(System.out::println);
System.out.println();
int row = r.nextInt(3) * 2;
int col = r.nextInt(3) * 2;
System.out.println("Move: " + row + ", " + col);
player = player == 'X' ? 'O' : 'X';
char val;
switch (val = recordMove(row, col, player)) {
case 'X' -> {
System.out.println("X wins!");
break outer;
}
case 'O' -> {
System.out.println("O wins!");
break outer;
}
case 0 -> {
System.out.println("Tie!");
break outer;
}
default -> {
}
}
}
commonGroupings.entrySet().forEach(System.out::println);
}
public char recordMove(int row, int col, char c) {
moveCount++;
for (TriGroup tri : commonGroupings.get(row + "" + col)) {
if (tri.group.length() > 2) {
// just ignore the row/col and try the next
continue;
}
// update group
tri.group += c;
if (tri.group.equals(c + "" + c + "" + c)) {
return c;
}
}
if (moveCount == MAX_MOVES) {
return 0;
}
return '#';
}
}
This version uses 2 auxiliary, private methods to make the code less repetitive, without changing the behavior of calling isGameOver. The main observation is that there is very little difference between checking for a win by O or checking for one by X - only a single character. So that becomes checkWins. The next observation is that there is a lot of repetition involved in checking 3 positions of the board that are next to each other. If you give me the char to expect, where to start, and where to look next (dcol and drow), that becomes allEqual.
Your code skips over uneven positions of the board; my code does not. I feel that it is an error to mix presentation ("how you show things to users") with model ("how you represent things internally"); so my code is not currently a drop-in replacement for yours, but can quickly be fixed to be such a replacement by tweaking values in checkWins (diagonal down would be 0, 0, 2, 2, and so on).
Note that, efficiency-wise, your code is probably faster. But I find this version to be much shorter, more readable, and therefore easier to debug & maintain.
private static boolean allEqual(char expected, char[][] b,
int row, int col, int drow, int dcol) {
for (int i=0; i<b[0].length; i++) {
if (b[row][col] != expected) return false;
row += drow;
col += dcol;
}
return true;
}
private static boolean checkWins(char playerChar, char[][]b) {
boolean win = allEqual(playerChar, b, 0, 0, 0, 1) // 1st row
|| allEqual(playerChar, b, 1, 0, 0, 1)
|| allEqual(playerChar, b, 2, 0, 0, 1) // 3rd row
|| allEqual(playerChar, b, 0, 0, 1, 0) // 1st col
|| allEqual(playerChar, b, 0, 1, 1, 0)
|| allEqual(playerChar, b, 0, 2, 1, 0) // 3rd col
|| allEqual(playerChar, b, 0, 0, 1, 1) // diagonal down
|| allEqual(playerChar, b, 2, 0, 1,-1); // diagonal up
return win;
}
public static boolean isGameOver(char[][] gameBoard) {
if (checkWins('X', gameBoard)) {
System.out.println("Player Wins!\n");
playerScore ++;
return true;
} else if (checkWins('O', gameBoard)) {
System.out.println("CPU Wins!\n");
cpuScore ++;
return true;
} else {
return false;
}
}
Take a look at this:
https://www.geeksforgeeks.org/tic-tac-toe-game-in-java/
Add the strings of gameBoard[x][y] and check them in a switch statement.
If the compound strings are equals to XXX or OOO, you can return the winner.
For your code something like this:
for (int a = 0; a < 8; a++) {
String line = null;
switch (a) {
case 0:
line = gameBoard[0][0] + gameBoard[0][1] + gameBoard[0][2];
break;
case 1:
line = gameBoard[1][0] + gameBoard[1][1] + gameBoard[1][2];
break;
case 2:
line = gameBoard[2][0] + gameBoard[2][1] + gameBoard[2][2];
break;
case 3:
line = gameBoard[0][0] + gameBoard[1][0] + gameBoard[2][0];
break;
case 4:
line = gameBoard[0][1] + gameBoard[1][1] + gameBoard[2][1];
break;
case 5:
line = gameBoard[0][2] + gameBoard[1][2] + gameBoard[2][2];
break;
case 6:
line = gameBoard[0][0] + gameBoard[1][1] + gameBoard[2][2];
break;
case 7:
line = gameBoard[0][2] + gameBoard[1][1] + gameBoard[2][0];
break;
}
//For X winner
if (line.equals("XXX")) {
return "X";
}
// For O winner
else if (line.equals("OOO")) {
return "O";
}
}

Java 2-D Arrays - Tic Tac Toe Game

This tic tac toe game is user vs computer based.
For this assignment, I'm supposed to fill in the missing pieces, which is just finishing up the main and moveAI methods(the other methods are already preset, I just need to call them); there is comments provided where I need to add code to make this tic tac toe game work. I believe that my main method that I have is almost done except for the 2 empty lines under the comments, I'm not sure what they mean by that and also I'm currently stuck on getting my moveAI method to work. This is where I'm supposed to generate random places where the computer will move after I put an X somewhere. I'm not sure how to write something that will check to make sure the spot where the computer moves is available and not already taken by the user. Thanks in advance for the help!
import java.util.Random;
import java.util.Scanner;
public class TicTacToeGame
{
public static void main ( String[] args )
{
Scanner in = new Scanner ( System.in );
char[][] board = new char[ 3 ][ 3 ];
int x, y = -1;
char winner = 'N';
// Initialize the board to spaces
// boolean noWinner = true;
char player = 'X';
for (int r = 0; x < board.length(); r++)
{
for (int c = 0; c < board.length(); c++)
{
board[r][c] = ' ';
}
}
// Print the game board
printBoard(board);
// Keep playing while the game isn't finished
while (winner == 'N')
{
while (x < 0 && x >2 && y <0 && y > 2)
{
System.out.println("Enter the row and column, separated by spaces: ");
x = in.nextInt();
y = in.nextInt();
}
// Get the location from the user and validate it
// Mark the position in the board according to the user's specified location
// Print the board after the user plays
printBoard(board);
// Check to see if the game is finished. If it is, break out of the loop.
// Have the AI make a move
moveAI(board);
// Print the board after the AI plays
printBoard(board);
// Check to see who the winner is
winner = checkWinner(board);
}
// If the winner is 'X' or 'O', print that, otherwise, it is a tie
if (winner == 'X')
System.out.println("X is the winner!");
else if (winner == 'O')
System.out.println("O is the winner!");
else
System.out.println("Tie");
}
/**
* Makes a move for the AI, and marks the board with an 'O'.
*
* #param board The game board
*/
public static void moveAI ( char[][] board )
{
int x,y = -1;
Random r = new Random();
x = r.nextInt(3);
y = r.nextInt(3);
boolean open = false;
// Validate that the random location generated is valid.
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
while (open == false)
{
if (board[r][c] != ' ')
while (x < 0 && x >2 && y < 0 && y > 2)
{
x = r.nextInt(3);
y = r.nextInt(3);
}
}
}
// Keep recalculating the location if the one generated is not
// if (board[r][c] != ' ')
//{
// an empty space.
System.out.print(" ");
// Be sure to mark the position in the board with an 'O'
board[][]=in.nextInt(3) + 'O';
}
/**
* Prints out the tic-tac-toe board
*
* #param board The game board
*/
public static void printBoard ( char[][] board )
{
// Box drawing unicode characters:
char a = '\u250c'; // U+250C : top-left
char b = '\u2510'; // U+2510 : top-right
char c = '\u2514'; // U+2514 : bottom-left
char d = '\u2518'; // U+2518 : bottom-right
char e = '\u252c'; // U+252C : top-vertical-connector
char f = '\u2534'; // U+2534 : bottom-vertical-connector
char g = '\u251c'; // U+251C : left-horizontal-connector
char h = '\u2524'; // U+2524 : right-horizontal-connector
char i = '\u253c'; // U+253C : center plus sign connector
char j = '\u2500'; // U+2500 : horizontal
char k = '\u2502'; // U+2502 : vertical
String l = j + "" + j + "" + j; // Three horizontals
// Print out the game board
System.out.printf ( "\n 0 1 2\n" +
" %c%s%c%s%c%s%c\n" +
"0 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"1 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n" +
"2 %c %c %c %c %c %c %c\n" +
" %c%s%c%s%c%s%c\n\n",
a, l, e, l, e, l, b,
k, board[0][0], k, board[0][1], k, board[0][2], k,
g, l, i, l, i, l, h,
k, board[1][0], k, board[1][1], k, board[1][2], k,
g, l, i, l, i, l, h,
k, board[2][0], k, board[2][1], k, board[2][2], k,
c, l, f, l, f, l, d );
}
/**
* Checks the result of the game
*
* #param board The game board
* #return 'X' if 'X' is the winner
* 'O' if 'O' is the winner
* 'T' if the game is a tie
* 'N' if the game isn't finished
*/
public static char checkWinner( char[][] board )
{
if (board[0][0] == 'X' && board[0][1] == 'X' && board[0][2] == 'X' || // Check row 0
board[1][0] == 'X' && board[1][1] == 'X' && board[1][2] == 'X' || // Check row 1
board[2][0] == 'X' && board[2][1] == 'X' && board[2][2] == 'X' || // Check row 2
board[0][0] == 'X' && board[1][0] == 'X' && board[2][0] == 'X' || // Check col 0
board[0][1] == 'X' && board[1][1] == 'X' && board[2][1] == 'X' || // Check col 1
board[0][2] == 'X' && board[1][2] == 'X' && board[2][2] == 'X' || // Check col 2
board[0][0] == 'X' && board[1][1] == 'X' && board[2][2] == 'X' || // Check diag \
board[0][2] == 'X' && board[1][1] == 'X' && board[2][0] == 'X' || // Check diag /
board[0][0] == 'x' && board[0][1] == 'x' && board[0][2] == 'x' || // Check row 0
board[1][0] == 'x' && board[1][1] == 'x' && board[1][2] == 'x' || // Check row 1
board[2][0] == 'x' && board[2][1] == 'x' && board[2][2] == 'x' || // Check row 2
board[0][0] == 'x' && board[1][0] == 'x' && board[2][0] == 'x' || // Check col 0
board[0][1] == 'x' && board[1][1] == 'x' && board[2][1] == 'x' || // Check col 1
board[0][2] == 'x' && board[1][2] == 'x' && board[2][2] == 'x' || // Check col 2
board[0][0] == 'x' && board[1][1] == 'x' && board[2][2] == 'x' || // Check diag \
board[0][2] == 'x' && board[1][1] == 'x' && board[2][0] == 'x') // Check diag /
{
return 'X';
}
else if (board[0][0] == 'O' && board[0][1] == 'O' && board[0][2] == 'O' || // Check row 0
board[1][0] == 'O' && board[1][1] == 'O' && board[1][2] == 'O' || // Check row 1
board[2][0] == 'O' && board[2][1] == 'O' && board[2][2] == 'O' || // Check row 2
board[0][0] == 'O' && board[1][0] == 'O' && board[2][0] == 'O' || // Check col 0
board[0][1] == 'O' && board[1][1] == 'O' && board[2][1] == 'O' || // Check col 1
board[0][2] == 'O' && board[1][2] == 'O' && board[2][2] == 'O' || // Check col 2
board[0][0] == 'O' && board[1][1] == 'O' && board[2][2] == 'O' || // Check diag \
board[0][2] == 'O' && board[1][1] == 'O' && board[2][0] == 'O' || // Check diag /
board[0][0] == 'o' && board[0][1] == 'o' && board[0][2] == 'o' || // Check row 0
board[1][0] == 'o' && board[1][1] == 'o' && board[1][2] == 'o' || // Check row 1
board[2][0] == 'o' && board[2][1] == 'o' && board[2][2] == 'o' || // Check row 2
board[0][0] == 'o' && board[1][0] == 'o' && board[2][0] == 'o' || // Check col 0
board[0][1] == 'o' && board[1][1] == 'o' && board[2][1] == 'o' || // Check col 1
board[0][2] == 'o' && board[1][2] == 'o' && board[2][2] == 'o' || // Check col 2
board[0][0] == 'o' && board[1][1] == 'o' && board[2][2] == 'o' || // Check diag \
board[0][2] == 'o' && board[1][1] == 'o' && board[2][0] == 'o') // Check diag /
{
return 'O';
}
boolean finished = true;
// If there is a blank space in the board, the game isn't finished yet
for (int i = 0; i < board.length; i++)
for (int j = 0; j < board[ i ].length; j++)
if (board[ i ][ j ] == ' ')
finished = false;
// If the board is finished and 'X' or 'O' wasn't returned, then it is a tie
// Otherwise, the game is not finished yet
if ( finished )
return 'T';
else
return 'N';
}
}
// Get the location from the user and validate it
It looks like you are already doing this in the preceding lines:
// you're validating the input here
while (x < 0 && x >2 && y <0 && y > 2)
{
System.out.println("Enter the row and column, separated by spaces: ");
// you're getting the location from the user here
x = in.nextInt();
y = in.nextInt();
}
You probably still need to check to see if the position the user entered is a position that's already been filled in. You need to look in the array for that. There's a comment in moveAI giving you a hint for this. You check if board[x][y] is a space.
Also, I think you have a bug there because you never reset the values for x and y, so the next time the player's turn comes around the loop will not be entered. x and y still have the last turn's values so the loop condition evaluates to false. I think you can solve this problem by using a do{...}while(...); loop instead of a while(...){...} loop.
// Mark the position in the board according to the user's specified location
This just means put an 'O' in board[x][y].
// Check to see if the game is finished. If it is, break out of the loop.
It looks like checkWinner does this. It returns 'T' if the game is done and 'N' if it's still going.
For the AI code, here is some pseudocode for what I think you should be doing:
1. generate random x, y coordinates
2. check if board[x][y] is already used (there's a comment that tells how)
3. if it's used, go back to 1, otherwise put an 'X' in that space and return
Also, you don't need the loops that check for while (x < 0 && x >2 && y < 0 && y > 2) in the AI code. Random.nextInt(3) already returns a number in the range 0-2.

How to increase efficiency by using for loops?

I am a beginner java programmer and I am making a simple TicTacToe game using 2D arrays and these are my if statements to check if player 1, or player 2 has won. I believe that this can be simplified by using for loop(s) however I do not understand how to use that method.
if ((grid[0][0] == 1 && grid[0][1] == 1 && grid[0][2] == 1)
|| (grid[1][0] == 1 && grid[1][1] == 1 && grid[1][2] == 1)
|| (grid[2][0] == 1 && grid[2][1] == 1 && grid[2][2] == 1)
|| (grid[0][0] == 1 && grid[1][1] == 1 && grid[2][2] == 1)
|| (grid[0][2] == 1 && grid[1][1] == 1 && grid[2][0] == 1)
|| (grid[0][0] == 1 && grid[1][0] == 1 && grid[2][0] == 1)
|| (grid[0][1] == 1 && grid[1][1] == 1 && grid[2][1] == 1)
|| (grid[0][2] == 1 && grid[1][2] == 1 && grid[2][2] == 1)
&& won == false) {
title.setText("X wins!");
won = true;
} else if ((grid[0][0] == 2 && grid[0][1] == 2 && grid[0][2] == 2)
|| (grid[1][0] == 2 && grid[1][1] == 2 && grid[1][2] == 2)
|| (grid[2][0] == 2 && grid[2][1] == 2 && grid[2][2] == 2)
|| (grid[0][0] == 2 && grid[1][1] == 2 && grid[2][2] == 2)
|| (grid[0][2] == 2 && grid[1][1] == 2 && grid[2][0] == 2)
|| (grid[0][0] == 2 && grid[1][0] == 2 && grid[2][0] == 2)
|| (grid[0][1] == 2 && grid[1][1] == 2 && grid[2][1] == 2)
|| (grid[0][2] == 2 && grid[1][2] == 2 && grid[2][2] == 2)
&& won == false) {
title.setText("O wins!");
won = true;
}
Below is the modified code that uses far less if statments and conditions.
public static boolean hasWon(int[][] grid) {
for (int a = 1; a <= 2; a++) {
for (int b = 0; b < grid.length; b++) {
// Checking for win in horizontal, then vertical, then diagonal
if (grid[b][0] == a && grid[b][1] == a && grid[b][2] == a) {
won = true;
} else if (grid[0][b] == a && grid[1][b] == a && grid[2][b] == a) {
won = true;
} else if ((grid[0][0] == a && grid[1][1] == a && grid[2][2] == a
|| (grid[0][2] == a && grid[1][1] == a && grid[2][0] == a))) {
won = true;
}
}
}
}
In order to help you reach a solution on your own I'll give you some hints for now.
Hint #1: Think about what it means to win. A player must get 3 of their tokens in a row - horizontal, vertical, or diagonal. Think about how that can be represented in your program.
Hint #2: Think about how you can break the problem into smaller more manageable pieces. Think about what each winning scenario has in common and separate that logic into a method that you can call multiple times.
Hint #3: Consider what makes each winning scenario unique and how you might use your grid to produce a representation of the spaces you want to examine that is easier to check for a win.
If you're not sure about how for loops work or other aspects of the Java Language you can find tutorials on Oracle's site
Yes you are right. For loops are the way to go. Here is one way you could implement it.
public class tictactoe {
public static void main(String[] args) {
int[][] grid = {{1, 2, 1},
{1, 2, 1},
{2, 0, 1}};
boolean won = hasWon(grid);
}
public static boolean hasWon(int[][] grid){
for (int player = 1; player <= 2; player++){
boolean playerWon = false;
for(int i = 0; i < 3; i++){
//Horizonal win
playerWon = (grid[i][0] == player && grid[i][1] == player && grid[i][2] == player) || playerWon;
//Vertical Win
playerWon = (grid[0][i] == player && grid[1][i] == player && grid[i][2] == player) || playerWon;
}
//Diagonal Win
playerWon = (grid[0][0] == player && grid[1][1] == player && grid[2][2] == player) || playerWon;
playerWon = (grid[0][2] == player && grid[1][1] == player && grid[2][0] == player) || playerWon;
if(playerWon){
if(player == 1){
System.out.println("X wins!");
return true;
}
else{
System.out.println("O wins!");
return true;
}
}
}
//neither have won
return false;
}
}
Not a direct answer for the question. (as this is not "check all at once" style)
To simplify,
1. Check when a cell is clicked.
2. Condition depends on the place of a cell which is clicked and who clicked the cell.
3. If someone wins, end the game.
code sample
// Part of codes.(not tested.)
// Each cell has three states (0, 1, or 2)
int player = 1; // (Not written here but) switch this each turn (1 or 2)
// In some place (activity.onCreate() etc)
{
// For on click event(0, 0)
cell_0_0.setOnClickListener(
new View.OnClickListener()
{
#Override
public void onClick(View v)
{
grid[0][0] = player;
final boolean isEnd = checkEnd_0_0();
if (isEnd) {
// Call some function to end the game.
// Calling title.setText() in game end function maybe good.
// (as not needed to write many times.)
if (player == 1) {
title.setText("X wins!");
} else {
title.setText("O wins!");
}
} else {
switchPlayer(); // Not written in this code.
}
}
};
);
...
}
// Call this on cell(0, 0) click event
// Returns true if someone wins.
boolean checkEnd_0_0() {
// Omit checking grid[0][0] is 1 or 2 as it is clear.
// Check horizontal.
if (grid[0][1] == player) {
if (grid[0][2] == player) {
return true; // This is the case shown in question.
}
}
// Check other cases (vertical, diagonal)
...
// No match.
return false;
}

(Java) Tic-Tac-Toe game using 2 dimensional Array

In class, our assignment is to create a two-dimensional array and create a tic-tac-toe game around it. I have everything done except displaying when the whole board is full and the game is a draw. I have tried a few things but I have not found the solution and I need some help... Here is my code:
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row, column;
char player = 'X';
//create 2 dimensional array for tic tac toe board
char[][] board = new char[3][3];
char ch = '1';
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
board[i][j] = ch++;
}
}
displayBoard(board);
while(!winner(board) == true){
//get input for row/column
System.out.println("Enter a row and column (0, 1, or 2); for player " + player + ":");
row = in.nextInt();
column = in.nextInt();
//occupied
while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}
//place the X
board[row][column] = player;
displayBoard(board);
if (winner(board)){
System.out.println("Player " + player + " is the winner!");
}
//time to swap players after each go.
if (player == 'O') {
player = 'X';
}
else {
player = 'O';
}
if (winner(board) == false) {
System.out.println("The game is a draw. Please try again.");
}
}
private static void displayBoard(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (j == board[i].length - 1) System.out.print(board[i][j]);
else System.out.print( board[i][j] + " | ");
}
System.out.println();
}
}
//method to determine whether there is an x or an o in the spot
public static Boolean winner(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'O' || board[i][j] == 'X') {
return false;
}
}
}
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
}
I want output saying that the board is full when it's full but I get nothing. This is the last line of my output and as you can see, my current strategy is not working as it continues to ask for input. -->
Enter a row and column (0, 1, or 2); for player X:
2 0
X | O | X
O | O | X
X | X | O
Enter a row and column (0, 1, or 2); for player O:
First off:
while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}
This will create a infinite loop because row and column shouldn't change you should ask for new input!
Also
public static Boolean winner(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] == 'O' || board[i][j] == 'X') {
return false;
}
}
}
As soon you hit 'O' or 'X' you will exit the Method with a false (no winner)
What you probably want to check is if every spot is occupied
public static Boolean winner(char[][] board){
//Boolean which is true until there is a empty spot
boolean occupied = true;
//loop and check if there is empty space or if its a draw
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
//Check if spot is not 'O' or not 'X' => empty
if (board[i][j] != 'O' || board[i][j] != 'X') {
occupied = false;
}
}
}
if(occupied)
return false;
//Check if someone won
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
This would now check if there is a winner or its a tie
Occupied == true == tie == return false
Winner == return true
But you have three states:
Win
Tie
NotFinished
With the changed Method you will NOT finish the game until you win.
Reason:
while(!winner(board) == true)
This makes the game run as long as there is NO winner
(winner() will be false because everything is occupied or there is no winner)
while(!false==true) => while(true)
You could write a method similar to winner but it only checks if the board has empty spots:
public static Boolean hasEmptySpot(char[][] board){
//loop and check if there is empty space
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;
}
}
}
return false;
}
//New code
while(hasEmptySpot(board) || !winner(board)){
//Your code for the game here
....
}
this would end the game when there is no empty spot left
After you finished the game you can call winner(board) and it will return if you tied or won!
By creating hasEmptySpot() you could change your winner method to
public static Boolean winner(char[][] board){
return (board[0][0] == board [0][1] && board[0][0] == board [0][2]) ||
(board[0][0] == board [1][1] && board[0][0] == board [2][2]) ||
(board[0][0] == board [1][0] && board[0][0] == board [2][0]) ||
(board[2][0] == board [2][1] && board[2][0] == board [2][2]) ||
(board[2][0] == board [1][1] && board[0][0] == board [0][2]) ||
(board[0][2] == board [1][2] && board[0][2] == board [2][2]) ||
(board[0][1] == board [1][1] && board[0][1] == board [2][1]) ||
(board[1][0] == board [1][1] && board[1][0] == board [1][2]);
}
Why?
Because you finished the game and you know there are only two possible outcomes Win or Tie.
I hope this helped you a little bit.
EDIT
Had a logic error myself!
First mistake:
you still need to check if there is a winner while the game is running forgot that point!
while(hasEmptySpot(board) || !winner(board)){
}
Now this will quit the game loop when there is a winner or no empty spots is left
Second mistake:
In hasEmptySpot()
if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;
not
if (board[i][j] != 'O' || board[i][j] != 'X') {
return true;
Fixed it in the upper examples.
I'm sorry for the inconvenience!
The most efficient way to do this is to keep a running count of how many spaces have been filled previously and increment that count each time a space is occupied. The board can be considered full when that count reaches 9.
If you're familiar with object-oriented programming, I think you'll find this easier to implement if you wrap your 2D array in a Board class.
Example:
public static class Board {
private char[][] spaces = new char[3][3];
private int numMoves = 0;
public void makeMove(int row, int col, char player) {
if (spaces[row][col] == 'X' || spaces[row][col] == 'O') {
System.out.println("This spot is occupied. Please try again");
} else {
spaces[row][col] = player;
numMoves++;
}
}
public boolean isFull() {
return numMoves == 9;
}
public boolean hasWinner() {
...
}
public void display() {
...
}
}
You could try to incorporate a new method such as the following:
public Boolean boardFull()
{
short count = 0;
for(short i = 0; i < 3; i++){
for(short j = 0; j < 3; j++){
if(board[i][j] == ‘O’ || board[i][j] == ’X’){
count++;
} else {
continue;
}
}
}
if(count == 9){
return true;
} else {
return false;
}
}
You could use an if statement to see if it returns true and then print something out if it does.
Solution
The code that's not working is your winner() method. It is always returning false if there is at least one cell occupied. You could proceed based on the last part of Nordiii's answer.
Extra problems
Cell-checking loop
Your code to check if a cell is occupied is going infinitely. You need to use an 'if' statement instead of a 'while' loop:
if(board[row][column] == 'X' || board[row][column] == 'O'){
System.out.println("This spot is occupied. Please try again");
continue;
}
Your old code got stuck always checking if 1 cell was occupied and it always returned true, which kept the loop alive and flooded your console. The continue statement will exit the current iteration of your other 'while' loop and start a new iteration, thus asking for new input.
Exceptions
Man, that's a lot of uncaught exceptions! If I mess up my input, pow! The whole thing fails. Just put a try block for your input-checking code:
try {
row = in.nextInt();
column = in.nextInt();
// Attempt to place player (an ArrayOutOfBoundsException could be thrown)
if(board[row][column] == 'X' || board[row][column] == 'O'){
System.out.println("This spot is occupied. Please try again");
continue;
}
board[row][column] = player;
} catch(Exception e){
System.out.println("I'm sorry, I didn't get that.");
continue;
}
This attempts to execute the code inside the try statement, and if someone inputs something incorrect, the exception gets 'caught' and a new iteration is created. Genius!
Although there are already some great answers I'd like to post another solution that is more generic in its logic to determine the winner. Currently you've hard-coded your some of the possible winning scenarios when you could write more generic logic for this.
As other answers have pointed out you want a method to check for unoccupied spaces in the board and this will tell you if there is a tie. I have implemented such a method in the code below along with the more generic winner logic.
Note that some methods are public to make it easier to test, they do not necessarily have to remain public.
import java.util.Scanner;
public class TicTacToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int row, column;
char player = 'X';
//create 2 dimensional array for tic tac toe board
char[][] board = new char[3][3];
char ch = '1';
for (int i = 0; i < 3; i++){
for (int j = 0; j < 3; j++) {
board[i][j] = ch++;
}
}
displayBoard(board);
while(!winner(board) == true){
//get input for row/column
System.out.println("Enter a row and column (0, 1, or 2); for player " + player + ":");
row = in.nextInt();
column = in.nextInt();
//occupied
while (board[row][column] == 'X' || board[row][column] == 'O') {
System.out.println("This spot is occupied. Please try again");
}
//place the X
board[row][column] = player;
displayBoard(board);
if (winner(board)){
System.out.println("Player " + player + " is the winner!");
}
//time to swap players after each go.
if (player == 'O') {
player = 'X';
}
else {
player = 'O';
}
if (winner(board) == false && !hasFreeSpace(board)) {
System.out.println("The game is a draw. Please try again.");
}
}
//Don't forget to close the scanner.
in.close();
}
public static void displayBoard(char[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board[i].length; j++) {
if (j == board[i].length - 1) System.out.print(board[i][j]);
else System.out.print( board[i][j] + " | ");
}
System.out.println();
}
}
/**
* Determines whether the board is completely occupied by X and O characters
* #param board the board to search through
* #return true if entire board is populated by X or O, false otherwise.
*/
public static boolean hasFreeSpace(char[][] board){
for (int i = 0; i< board.length; i++) {
for (int j = 0; j < board[0].length; j++) {
if (board[i][j] != 'O' && board[i][j] != 'X') {
return true;
}
}
}
return false;
}
//method to determine whether there is a winner
public static boolean winner(char[][] board){
return isHorizontalWin(board) || isVerticalWin(board) || isDiagonalWin(board);
}
/**
* Determines if there is a winner by checking each row for consecutive
* matching tokens.
* #return true if there is a winner horizontally, false otherwise.
*/
private static boolean isHorizontalWin(char[][] board) {
for(int row = 0; row < board.length; row++){
if(isWin(board[row]))
return true;
}
return false;
}
/**
* Determines whether all of the buttons in the specified array have the
* same text and that the text is not empty string.
* #param lineToProcess an array of buttons representing a line in the grid
* #return true if all buttons in the array have the same non-empty text, false otherwise.
*/
private static boolean isWin(char[] lineToProcess) {
boolean foundWin = true;
char prevChar = '-';
for(char character: lineToProcess) {
if(prevChar == '-')
prevChar = character;
if ('O' != character && 'X' != character) {
foundWin = false;
break;
} else if (prevChar != character) {
foundWin = false;
break;
}
}
return foundWin;
}
/**
* Determines whether there is a winner by checking column for consecutive
* matching tokens.
* #return true if there is a vertical winner, false otherwise.
*/
private static boolean isVerticalWin(char[][] board) {
char[] column = null;
//assuming all rows have same legnth (same number of cols in each row), use first row
for(int col = 0; col < board[0].length; col++){
column = new char[board[0].length];
for(int row = 0; row < column.length; row++){
column[row] = board[row][col];
}
if(isWin(column))
return true;
}
return false;
}
/**
* Determines if there is a winner by checking each diagonal for consecutive
* matching tokens.
* #return true if a diagonal winner exists, false otherwise.
*/
private static boolean isDiagonalWin(char[][] board) {
int row = 0, col = 0;
int cols = board.length;
int rows = board[0].length; //assuming all rows are equal length so just use the first one
//Create a one-dimensional array to represent the diagonal. Use the lesser
// of the rows or columns to set its size. If the grid is rectangular then
// a diagonal will always be the size of the lesser of its two dimensions.
int size = rows < cols ? rows : cols;
char[] diagonal = new char[size];
//Since we know the grid is a square we really could just check one of
// these - either row or col, but I left both in here anyway.
while (row < rows && col < cols) {
diagonal[col] = board[row][col];
row++;
col++;
}
if (isWin(diagonal)) {
return true;
}
row = rows - 1;
col = 0;
diagonal = new char[size];
while (row >=0 && col < cols) {
diagonal[col] = board[row][col];
row--;
col++;
}
return isWin(diagonal);
}
}

Tic-tac-toe java: what to do if the user inputs a position that's already taken?

Here is the code I have written so far. My game works just fine but the problem I have is if the user inputs a position that's already taken, yes it keeps asking the user for input, but what i would want it to do it say: This position is unavailable and then reask the user until it's good. I thought about a for loop, but with the loop I have, it says it's unavailable even if it is not...I have been on this for days. If someone could give me some hints I would appreciate it.
import java.util.Scanner;
public class A3Q1_40011419 {
//Creating the board for the user input
static char [][] board = {{'1','2','3'}, {'4','5','6'},{'7','8','9'}};
static Scanner keyboard = new Scanner (System.in);
//Keeping track of the turns
public static int turns = 0;
//Drawing the board
public static void drawBoard() {
int row = 0;
int col = 0;
int turn = 0;
for (row = 0; row < board.length; row++){
for (col = 0; col < board[row].length; col++){
System.out.print(board[row][col]);
System.out.print(" ");
}
System.out.println();
}
}
//Asking user for input
public static void getInput (int playerNum) {
char piece;
char location = ' ';
if (playerNum != 1){
System.out.println("Player X - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'X';
}
else{
System.out.println("Player O - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'O';
}
if (location == '1')
if (board[0][0]=='1')
board[0][0] = piece;
else getInput(playerNum);
else if (location == '2')
if (board[0][1] == '2')
board[0][1] = piece;
else getInput(playerNum);
else if (location == '3')
if (board[0][2] == '3')
board[0][2] = piece;
else getInput(playerNum);
else if (location == '4')
if (board[1][0] == '4')
board[1][0] = piece;
else getInput(playerNum);
else if (location == '5')
if (board[1][1] == '5')
board[1][1] = piece;
else getInput(playerNum);
else if (location == '6')
if (board[1][2] == '6')
board[1][2] = piece;
else getInput(playerNum);
else if (location == '7')
if (board[2][0] == '7')
board[2][0] = piece;
else getInput(playerNum);
else if (location == '8')
if (board[2][1] == '8')
board[2][1] = piece;
else getInput(playerNum);
else if (location == '9')
if (board[2][2] == '9')
board[2][2] = piece;
else getInput(playerNum);
else if (location != '1' || location !='2' || location !='3' || location != '4' || location !='5' || location !='6' || location != '7' || location != '8'|| location !='9'){
System.out.println("That is not a valid position - must be between 1 and 9 inclusive.");
getInput(playerNum);
}
for (int i = 0; i<board.length; i++){
for (int j = 0; j<board.length; j++)
/*if (location == '1' || location == '2' || location == '3'|| location == '4' || location == '5'|| location == '6' || location == '7' || location == '8' || location == '9')*/
if (board[i][j] == 'X' || board[i][j] == 'O'){
System.out.println("Position not available"); //This is was i tried to do so far to check is the position was available on the board. Not working
}
getInput(playerNum);
}
}
//Checking if position available
public static void isAvailable(){ //This is a method i thought of creating to check if the position was available but I have not used it yet.
char location_1 = ' ';
char location_2 = ' ';
char location_3 = ' ';
char location_4 = ' ';
char location_5 = ' ';
char location_6 = ' ';
char location_7 = ' ';
char location_8 = ' ';
char location_9 = ' ';
}
/*Checking if there is a winner by checking if the cells in the board are equal to each other
*and checking if the cell isn't empty. If one isn't empty and they're all equal, then
none of them is empty.*/
public static char isWinner() {
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0]!='1')
return board[0][0];
else if (board[0][0]==board[0][1] && board[0][0]==board[0][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] !='3')
return board[0][2];
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != '3')
return board[0][2];
else if (board[2][0] == board [2][1] && board[2][0] == board[2][2] && board[2][0] != '7')
return board[2][0];
else if (board[1][0] == board [1][1] && board[1][0] == board[1][2] && board[1][0] != '4')
return board[1][0];
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != '2')
return board[0][1];
else
return 'Y';
}
//New board for a new game
public static void newBoard() {
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
//Main method
public static void main(String[] args) {
int game = 0;
int playerNum = 1;
String answer;
do {
turns = 0;
newBoard();
while (isWinner() == 'Y') {
drawBoard();
isWinner();
playerNum *= -1;
getInput(playerNum);
turns++;
System.out.println(turns);
if (turns == 9) {
break;
}
}
drawBoard();
if (isWinner() == 'X')
System.out.println("Player X wins");
else if (isWinner()=='O')
System.out.println("Player O wins");
else
System.out.println("It's a tie!");
System.out.println("Would you like to play another game?");
answer = keyboard.next().toLowerCase();
}
while (answer.equals("yes"));
}
}
I would suggest:
You move your check for available position before inserting the new input.
Instead of a loop to check all positions of the grid, you check only the position the player is asking.
Please find a working code below:
public class A3Q1_40011419 {
//Creating the board for the user input
static char [][] board = {{'1','2','3'}, {'4','5','6'},{'7','8','9'}};
static Scanner keyboard = new Scanner (System.in);
//Keeping track of the turns
public static int turns = 0;
//Drawing the board
public static void drawBoard() {
int row = 0;
int col = 0;
int turn = 0;
for (row = 0; row < board.length; row++){
for (col = 0; col < board[row].length; col++){
System.out.print(board[row][col]);
System.out.print(" ");
}
System.out.println();
}
}
//Asking user for input
public static void getInput (int playerNum) {
char piece;
char location = ' ';
if (playerNum != 1){
System.out.println("Player X - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'X';
}
else{
System.out.println("Player O - Enter the position you wish to mark.");
location = keyboard.next().charAt(0);
piece = 'O';
}
//------------------------
// Your new check
int locationInt = Integer.parseInt("" + location);
int x = (locationInt-1)/3;
int y = ((locationInt-1)%3);
if (board[x][y] == 'X' || board[x][y] == 'O'){
System.out.println("Position not available"); //This is was i tried to do so far to check is the position was available on the board. Not working
getInput(playerNum);
return;
}
//------------------------
if (location == '1')
if (board[0][0]=='1')
board[0][0] = piece;
else getInput(playerNum);
else if (location == '2')
if (board[0][1] == '2')
board[0][1] = piece;
else getInput(playerNum);
else if (location == '3')
if (board[0][2] == '3')
board[0][2] = piece;
else getInput(playerNum);
else if (location == '4')
if (board[1][0] == '4')
board[1][0] = piece;
else getInput(playerNum);
else if (location == '5')
if (board[1][1] == '5')
board[1][1] = piece;
else getInput(playerNum);
else if (location == '6')
if (board[1][2] == '6')
board[1][2] = piece;
else getInput(playerNum);
else if (location == '7')
if (board[2][0] == '7')
board[2][0] = piece;
else getInput(playerNum);
else if (location == '8')
if (board[2][1] == '8')
board[2][1] = piece;
else getInput(playerNum);
else if (location == '9')
if (board[2][2] == '9')
board[2][2] = piece;
else getInput(playerNum);
else if (location != '1' || location !='2' || location !='3' || location != '4' || location !='5' || location !='6' || location != '7' || location != '8'|| location !='9'){
System.out.println("That is not a valid position - must be between 1 and 9 inclusive.");
getInput(playerNum);
}
}
//Checking if position available
public static void isAvailable(){ //This is a method i thought of creating to check if the position was available but I have not used it yet.
char location_1 = ' ';
char location_2 = ' ';
char location_3 = ' ';
char location_4 = ' ';
char location_5 = ' ';
char location_6 = ' ';
char location_7 = ' ';
char location_8 = ' ';
char location_9 = ' ';
}
/*Checking if there is a winner by checking if the cells in the board are equal to each other
*and checking if the cell isn't empty. If one isn't empty and they're all equal, then
none of them is empty.*/
public static char isWinner() {
if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[0][0]!='1')
return board[0][0];
else if (board[0][0]==board[0][1] && board[0][0]==board[0][2] && board[0][0]!='1')
return board[0][0];
else if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[0][2] !='3')
return board[0][2];
else if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != '3')
return board[0][2];
else if (board[2][0] == board [2][1] && board[2][0] == board[2][2] && board[2][0] != '7')
return board[2][0];
else if (board[1][0] == board [1][1] && board[1][0] == board[1][2] && board[1][0] != '4')
return board[1][0];
else if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != '2')
return board[0][1];
else
return 'Y';
}
//New board for a new game
public static void newBoard() {
board[0][0] = '1';
board[0][1] = '2';
board[0][2] = '3';
board[1][0] = '4';
board[1][1] = '5';
board[1][2] = '6';
board[2][0] = '7';
board[2][1] = '8';
board[2][2] = '9';
}
//Main method
public static void main(String[] args) {
int game = 0;
int playerNum = 1;
String answer;
do {
turns = 0;
newBoard();
while (isWinner() == 'Y') {
drawBoard();
isWinner();
playerNum *= -1;
getInput(playerNum);
turns++;
System.out.println(turns);
if (turns == 9) {
break;
}
}
drawBoard();
if (isWinner() == 'X')
System.out.println("Player X wins");
else if (isWinner()=='O')
System.out.println("Player O wins");
else
System.out.println("It's a tie!");
System.out.println("Would you like to play another game?");
answer = keyboard.next().toLowerCase();
}
while (answer.equals("yes"));
}
}

Categories