\$\begingroup\$
\$\endgroup\$
3
I am trying to create a Tic Tac Toe game in Java using a 2 dimensional array. The computer has to do the smartest move.
/* This program creates an interactive game of Tic-Tac-Toe
* The computer is able to play intelligently and block the user and try to win
* It uses a 2D array to create a grid for the game
* The user makes their moves using a coordinate system
*/
//for the Scanner
import java.util.*;
public class TicTacToe {
private static boolean first = false;
private static char[][] board = new char[3][3];
private static int aiScore = 0;
private static int playerScore = 0;
public static void main(String[] args){
intro();
}
public static void intro(){
Scanner console = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.println("Welcome to Tic-Tac-Toe");
System.out.println("To play this classic game against the computer,");
System.out.println("You will choose where to play using coordinates(1-3)");
System.out.println("");
System.out.println("Would you like to go first?");
String goFirst = console.nextLine();
if(goFirst.substring(0,1).equalsIgnoreCase("y")){
first = true;
}
initializeBoard();
getBoard();
startGame(console);
}
public static void startGame(Scanner console){
//clears the board to start the game
if(first == true){
userTurn(console);
}else{
compTurn(console);
}
}
//allows user to play
public static void userTurn(Scanner console){
System.out.println("Where would you like to play?");
int r = console.nextInt() - 1;
int c = console.nextInt() - 1;
if(board[r][c] == ' '){
board[r][c] = 'X';
if(board[0][0] == 'X'){
getBoard();
playerScore += 1;
checkScoreUser(console);
} else if(board[0][2] == 'X'){
getBoard();
playerScore += 3;
checkScoreUser(console);
} else if(board[2][0] == 'X'){
getBoard();
playerScore += 1000;
checkScoreUser(console);
} else if(board[2][2] == 'X'){
getBoard();
playerScore += 3000;
checkScoreUser(console);
} else if(board[1][1] == 'X'){
getBoard();
playerScore += 200;
checkScoreUser(console);
} else if(board[0][1] == 'X'){
getBoard();
playerScore += 2;
checkScoreUser(console);
} else if(board[1][0] == 'X'){
getBoard();
playerScore += 100;
checkScoreUser(console);
} else if(board[1][2] == 'X'){
getBoard();
playerScore += 300;
checkScoreUser(console);
} else if(board[2][1] == 'X'){
getBoard();
playerScore += 2000;
checkScoreUser(console);
}
} else if (board[r][c] == 'X' || board[r][c] == 'O'){
first = true;
System.out.println("Please choose an empty space");
startGame(console);
}
System.out.println("Cat's Game");
}
//allows computer to play
public static void compTurn(Scanner console){
System.out.println("Now it's my turn");
System.out.println("");
if(board[0][0] == ' '){
board[0][0] = 'O';
getBoard();
userTurn(console);
aiScore += 1;
} else if(board[0][2] == ' '){
board[0][2] = 'O';
getBoard();
userTurn(console);
aiScore += 3;
} else if(board[2][0] == ' '){
board[2][0] = 'O';
getBoard();
userTurn(console);
aiScore += 1000;
} else if(board[2][2] == ' '){
board[2][2] = 'O';
getBoard();
userTurn(console);
aiScore += 3000;
} else if(board[1][1] == ' '){
board[1][1] = 'O';
getBoard();
userTurn(console);
aiScore += 200;
} else if(board[0][1] == ' '){
board[0][1] = 'O';
getBoard();
userTurn(console);
aiScore += 2;
} else if(board[1][0] == ' '){
board[1][0] = 'O';
getBoard();
userTurn(console);
aiScore += 100;
} else if(board[1][2] == ' '){
board[1][2] = 'O';
getBoard();
userTurn(console);
aiScore += 300;
} else if(board[2][1] == ' '){
board[2][1] = 'O';
getBoard();
userTurn(console);
aiScore += 2000;
}
}
/*public static void checkUser(Scanner console){
if(playerScore == 3){
board[0][2] = 'O';
} else if (playerScore ==)
}*/
//score checkers
public static void checkScoreUser(Scanner console){
if(aiScore == 3201 || aiScore == 1203 || aiScore == 6 ||
aiScore == 600 || aiScore == 6000 || aiScore == 1101 ||
aiScore == 2202 || aiScore == 3303){
System.out.println("I win!!! Hahahahahaha!!!!!!");
intro();
} else if (playerScore == 3201 || playerScore == 1203 || playerScore == 6 ||
playerScore == 600 || playerScore == 6000 || playerScore == 1101 ||
playerScore == 2202 || playerScore == 3303){
System.out.println("You won!");
intro();
} else {
compTurn(console);
}
}
public static void checkScoreComp(Scanner console){
if(aiScore == 3201 || aiScore == 1203 || aiScore == 6 ||
aiScore == 600 || aiScore == 6000 || aiScore == 1101 ||
aiScore == 2202 || aiScore == 3303){
System.out.println("I win!!! Hahahahahaha!!!!!!");
intro();
} else if (playerScore == 3201 || playerScore == 1203 || playerScore == 6 ||
playerScore == 600 || playerScore == 6000 || playerScore == 1101 ||
playerScore == 2202 || playerScore == 3303){
System.out.println("You won!");
intro();
} else {
userTurn(console);
}
}
//board stuff
//initializes board and fills it with black spaces
public static void initializeBoard(){
for(int r = 0; r < 3; r++){
for(int c = 0; c < 3; c++){
board[r][c] = ' ';
}
}
}
//prints the actual board out
public static void getBoard(){
String bar = " =============";
System.out.println(" 1 2 3");
System.out.println(bar);
System.out.println("1 " + board[0][0] + " | " + board[0][1] + " | " + board[0][2]);
System.out.println(bar);
System.out.println("2 " + board[1][0] + " | " + board[1][1] + " | " + board[1][2]);
System.out.println(bar);
System.out.println("3 " + board[2][0] + " | " + board[2][1] + " | " + board[2][2]);
System.out.println(bar);
System.out.println("");
}
}
200_success
146k22 gold badges190 silver badges478 bronze badges
asked Jan 29, 2017 at 21:30
-
1\$\begingroup\$ Welcome to Code Review! Please keep in mind that implementing new features (as you asked in the initial version of the question) is out of our scope, as explained on on-topic. We can review the current code and help you make it better. \$\endgroup\$janos– janos2017年01月30日 06:42:56 +00:00Commented Jan 30, 2017 at 6:42
-
\$\begingroup\$ "I am trying to create a Tic Tac Toe game in Java" Did you succeed? Does it work as intended? \$\endgroup\$Mast– Mast ♦2017年01月30日 08:34:31 +00:00Commented Jan 30, 2017 at 8:34
-
\$\begingroup\$ What you may and may not do after receiving answers. I've rolled back Rev 5 → 4. \$\endgroup\$200_success– 200_success2017年01月31日 03:10:38 +00:00Commented Jan 31, 2017 at 3:10
1 Answer 1
\$\begingroup\$
\$\endgroup\$
This transcript below shows the win detection is buggy.
$ java TicTacToe Welcome to Tic-Tac-Toe To play this classic game against the computer, You will choose where to play using coordinates(1-3) Would you like to go first? n 1 2 3 =わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ 1 | | ============= 2 | | ============= 3 | | ============= Now it's my turn 1 2 3 =わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ 1 O | | ============= 2 | | ============= 3 | | ============= Where would you like to play? 1 3 1 2 3 =わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ 1 O | | X ============= 2 | | ============= 3 | | ============= Now it's my turn 1 2 3 =わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ 1 O | | X ============= 2 | | ============= 3 O | | ============= Where would you like to play? 2 1 1 2 3 =わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ=わ 1 O | | X ============= 2 X | | ============= 3 O | | ============= You won!
Huh, what? That is way too early for the AI to forfeit!
answered Jan 30, 2017 at 7:43
lang-java