Tic Tac Toe game becomes more inefficient as time goes on
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
2
down vote
favorite
I designed a tic tac toe using 4 classes. The game class being the main class, the board class being the board, the player class being the decision making, and the AI class keeping the previous game combinations. I found that this code was very inefficient especially as the number of games increases to the hundreds. This game was set specifically for two computers to play against each other. It's not really AI, but it if the same conditions apply (all the other spots are the same), then it won't make the same mistake. What can I do to improve my code efficiency since there are over 250k possible combinations for tic tac toe?
public class Game
public static void main(String args)
int playerWin = 0;
int computerWin = 0;
int ties = 0;
int totalGame = 1;
System.out.println("Welcome to Tic Tac Toe");
Board bgame = new Board();
Player p1 = new Player();
Player p2 = new Player();
bgame.createGrid();
while (true)
bgame.checkWinDiag()) && bgame.playerWin)
System.out.println("You have won!");
bgame.printGrid();
AI lossSave = new AI(bgame.getMap());
p2.addLoss(lossSave);
totalGame++;
playerWin++;
bgame = new Board();
bgame.createGrid();
else if ((bgame.checkWinRow()
import java.io.*;
import java.util.*;
public class Player
public Player()
public int playerMove(String tempMap, int playerTurn)
int ret = new int[2];
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Scanner usr_in = new Scanner(System.in);
String u_in = usr_in.nextLine();
String rAndC = new String[2];
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
while (!(tempMap[ret[1]][ret[0]] == " "))
System.out.println("That spot is already taken, please try again.");
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Board.printGrid(tempMap);
u_in = usr_in.nextLine();
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
return ret;
public int computerMove(String tempMap, int playerTurn)
String aiCheck = new String[3][3];
String currentPlayer = "";
if (playerTurn % 2 == 0)
currentPlayer = "O";
else
currentPlayer = "X";
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
aiCheck[i][a] = tempMap[i][a];
System.out.println("The computer's turn: ");
int grid = new int[2];
int row;
int column;
do
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
while (tempMap[column][row] != " ");
int aiCounter = -1;
if (rem.size() == 0)
else
aiCheck[row][column] = currentPlayer;
while (aiCounter < rem.size() - 1)
aiCounter++;
if (!((AI)rem.get(aiCounter)).compareIt(aiCheck))
continue;
else
aiCounter = 0;
do
aiCheck[row][column] = tempMap[row][column];
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
aiCheck[row][column] = currentPlayer;
while (tempMap[column][row] != " " && !(((AI)rem.get(aiCounter)).compareIt(aiCheck)));
grid[0] = row;
grid[1] = column;
Board.printGrid(tempMap);
return grid;
public void addLoss(AI a)
rem.add(a);
public List retEv()
return rem;
public List rem = new ArrayList();
public class Board
// Board Size is 3 x 3
public Board()
public void createGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
map[r][c] = " ";
public void setBoard(String test)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
map[i][a] = test[i][a];
// Method to Print Graphics
public void printGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
c == 1)
");
if (r == 0
System.out.println();
public static void printGrid(String tGrid)
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
System.out.print(tGrid[r][c]);
if (c == 0
if (r == 0
System.out.println();
// Method to set a position to a X or O
public void setPosGrid(int rAndC)
int row = rAndC[1];
int column = rAndC[0];
if (turn %2 == 0)
map[row][column] = " O ";
else
map[row][column] = " X ";
turn++;
public void turnNum()
if (turn % 2 == 0)
System.out.println("It is the computer's turn, O");
else
System.out.println("It is the player's turn, X");
public String getMap()
return map;
public int getNum()
return turn;
public String getRow(int r)
String row = new String[3];
for (int i = 0; i < 3; i++)
row[i] = map[r][i];
return row;
public String getColumn(int c)
String column = new String[3];
for (int i = 0; i < 3; i++)
column[i] = map[i][c];
return column;
//check win using row & columns method
public boolean checkWinRow()
for (int i = 0; i < 3; i++)
String test = getRow(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinCol()
for (int i = 0; i < 3; i++)
String test = getColumn(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinDiag()
String s = map[1][1];
if (map[0][0].equals(s) && map[2][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else if (map[2][0].equals(s) && map[0][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else
return false;
public boolean checkTie()
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3;a ++)
if (map[i][a].equals(" "))
return false;
return true;
public boolean playerWin = false;
private int turn = 1;
private String map = new String[3][3];
public class AI
public AI(String temp)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
perm[i][a] = temp[i][a];
public boolean compareIt(String check)
boolean notSame = true;
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
if (perm[i][a].equals(check[i][a]))
continue;
else
notSame = false;
return notSame;
return true;
public String retIt()
return perm;
public String perm = new String[3][3];
java performance algorithm tic-tac-toe ai
add a comment |Â
up vote
2
down vote
favorite
I designed a tic tac toe using 4 classes. The game class being the main class, the board class being the board, the player class being the decision making, and the AI class keeping the previous game combinations. I found that this code was very inefficient especially as the number of games increases to the hundreds. This game was set specifically for two computers to play against each other. It's not really AI, but it if the same conditions apply (all the other spots are the same), then it won't make the same mistake. What can I do to improve my code efficiency since there are over 250k possible combinations for tic tac toe?
public class Game
public static void main(String args)
int playerWin = 0;
int computerWin = 0;
int ties = 0;
int totalGame = 1;
System.out.println("Welcome to Tic Tac Toe");
Board bgame = new Board();
Player p1 = new Player();
Player p2 = new Player();
bgame.createGrid();
while (true)
bgame.checkWinDiag()) && bgame.playerWin)
System.out.println("You have won!");
bgame.printGrid();
AI lossSave = new AI(bgame.getMap());
p2.addLoss(lossSave);
totalGame++;
playerWin++;
bgame = new Board();
bgame.createGrid();
else if ((bgame.checkWinRow()
import java.io.*;
import java.util.*;
public class Player
public Player()
public int playerMove(String tempMap, int playerTurn)
int ret = new int[2];
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Scanner usr_in = new Scanner(System.in);
String u_in = usr_in.nextLine();
String rAndC = new String[2];
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
while (!(tempMap[ret[1]][ret[0]] == " "))
System.out.println("That spot is already taken, please try again.");
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Board.printGrid(tempMap);
u_in = usr_in.nextLine();
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
return ret;
public int computerMove(String tempMap, int playerTurn)
String aiCheck = new String[3][3];
String currentPlayer = "";
if (playerTurn % 2 == 0)
currentPlayer = "O";
else
currentPlayer = "X";
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
aiCheck[i][a] = tempMap[i][a];
System.out.println("The computer's turn: ");
int grid = new int[2];
int row;
int column;
do
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
while (tempMap[column][row] != " ");
int aiCounter = -1;
if (rem.size() == 0)
else
aiCheck[row][column] = currentPlayer;
while (aiCounter < rem.size() - 1)
aiCounter++;
if (!((AI)rem.get(aiCounter)).compareIt(aiCheck))
continue;
else
aiCounter = 0;
do
aiCheck[row][column] = tempMap[row][column];
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
aiCheck[row][column] = currentPlayer;
while (tempMap[column][row] != " " && !(((AI)rem.get(aiCounter)).compareIt(aiCheck)));
grid[0] = row;
grid[1] = column;
Board.printGrid(tempMap);
return grid;
public void addLoss(AI a)
rem.add(a);
public List retEv()
return rem;
public List rem = new ArrayList();
public class Board
// Board Size is 3 x 3
public Board()
public void createGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
map[r][c] = " ";
public void setBoard(String test)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
map[i][a] = test[i][a];
// Method to Print Graphics
public void printGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
c == 1)
");
if (r == 0
System.out.println();
public static void printGrid(String tGrid)
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
System.out.print(tGrid[r][c]);
if (c == 0
if (r == 0
System.out.println();
// Method to set a position to a X or O
public void setPosGrid(int rAndC)
int row = rAndC[1];
int column = rAndC[0];
if (turn %2 == 0)
map[row][column] = " O ";
else
map[row][column] = " X ";
turn++;
public void turnNum()
if (turn % 2 == 0)
System.out.println("It is the computer's turn, O");
else
System.out.println("It is the player's turn, X");
public String getMap()
return map;
public int getNum()
return turn;
public String getRow(int r)
String row = new String[3];
for (int i = 0; i < 3; i++)
row[i] = map[r][i];
return row;
public String getColumn(int c)
String column = new String[3];
for (int i = 0; i < 3; i++)
column[i] = map[i][c];
return column;
//check win using row & columns method
public boolean checkWinRow()
for (int i = 0; i < 3; i++)
String test = getRow(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinCol()
for (int i = 0; i < 3; i++)
String test = getColumn(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinDiag()
String s = map[1][1];
if (map[0][0].equals(s) && map[2][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else if (map[2][0].equals(s) && map[0][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else
return false;
public boolean checkTie()
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3;a ++)
if (map[i][a].equals(" "))
return false;
return true;
public boolean playerWin = false;
private int turn = 1;
private String map = new String[3][3];
public class AI
public AI(String temp)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
perm[i][a] = temp[i][a];
public boolean compareIt(String check)
boolean notSame = true;
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
if (perm[i][a].equals(check[i][a]))
continue;
else
notSame = false;
return notSame;
return true;
public String retIt()
return perm;
public String perm = new String[3][3];
java performance algorithm tic-tac-toe ai
There areimport
statements in the middle of the code, which makes me wonder of you copied multiple source files into a single code box. Also the indentation is very strange.
â Martin R
May 21 at 11:31
1
Every time you lose a game you're adding a new object to a list. That list will get bigger and bigger, so walking through it is going to take more and more time. You should consider a rules-based AI instead.
â Eric Stein
May 21 at 14:47
Can you elaborate on a rules-based AI? The list will become too big and become inefficient. I'm not sure how I would implement a rule based AI to work with the code. My indentation is weird sometimes, I'm not sure why. I believe it's because I didn't split the code into separate classes correctly.
â XTImpossible
May 23 at 22:57
add a comment |Â
up vote
2
down vote
favorite
up vote
2
down vote
favorite
I designed a tic tac toe using 4 classes. The game class being the main class, the board class being the board, the player class being the decision making, and the AI class keeping the previous game combinations. I found that this code was very inefficient especially as the number of games increases to the hundreds. This game was set specifically for two computers to play against each other. It's not really AI, but it if the same conditions apply (all the other spots are the same), then it won't make the same mistake. What can I do to improve my code efficiency since there are over 250k possible combinations for tic tac toe?
public class Game
public static void main(String args)
int playerWin = 0;
int computerWin = 0;
int ties = 0;
int totalGame = 1;
System.out.println("Welcome to Tic Tac Toe");
Board bgame = new Board();
Player p1 = new Player();
Player p2 = new Player();
bgame.createGrid();
while (true)
bgame.checkWinDiag()) && bgame.playerWin)
System.out.println("You have won!");
bgame.printGrid();
AI lossSave = new AI(bgame.getMap());
p2.addLoss(lossSave);
totalGame++;
playerWin++;
bgame = new Board();
bgame.createGrid();
else if ((bgame.checkWinRow()
import java.io.*;
import java.util.*;
public class Player
public Player()
public int playerMove(String tempMap, int playerTurn)
int ret = new int[2];
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Scanner usr_in = new Scanner(System.in);
String u_in = usr_in.nextLine();
String rAndC = new String[2];
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
while (!(tempMap[ret[1]][ret[0]] == " "))
System.out.println("That spot is already taken, please try again.");
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Board.printGrid(tempMap);
u_in = usr_in.nextLine();
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
return ret;
public int computerMove(String tempMap, int playerTurn)
String aiCheck = new String[3][3];
String currentPlayer = "";
if (playerTurn % 2 == 0)
currentPlayer = "O";
else
currentPlayer = "X";
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
aiCheck[i][a] = tempMap[i][a];
System.out.println("The computer's turn: ");
int grid = new int[2];
int row;
int column;
do
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
while (tempMap[column][row] != " ");
int aiCounter = -1;
if (rem.size() == 0)
else
aiCheck[row][column] = currentPlayer;
while (aiCounter < rem.size() - 1)
aiCounter++;
if (!((AI)rem.get(aiCounter)).compareIt(aiCheck))
continue;
else
aiCounter = 0;
do
aiCheck[row][column] = tempMap[row][column];
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
aiCheck[row][column] = currentPlayer;
while (tempMap[column][row] != " " && !(((AI)rem.get(aiCounter)).compareIt(aiCheck)));
grid[0] = row;
grid[1] = column;
Board.printGrid(tempMap);
return grid;
public void addLoss(AI a)
rem.add(a);
public List retEv()
return rem;
public List rem = new ArrayList();
public class Board
// Board Size is 3 x 3
public Board()
public void createGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
map[r][c] = " ";
public void setBoard(String test)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
map[i][a] = test[i][a];
// Method to Print Graphics
public void printGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
c == 1)
");
if (r == 0
System.out.println();
public static void printGrid(String tGrid)
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
System.out.print(tGrid[r][c]);
if (c == 0
if (r == 0
System.out.println();
// Method to set a position to a X or O
public void setPosGrid(int rAndC)
int row = rAndC[1];
int column = rAndC[0];
if (turn %2 == 0)
map[row][column] = " O ";
else
map[row][column] = " X ";
turn++;
public void turnNum()
if (turn % 2 == 0)
System.out.println("It is the computer's turn, O");
else
System.out.println("It is the player's turn, X");
public String getMap()
return map;
public int getNum()
return turn;
public String getRow(int r)
String row = new String[3];
for (int i = 0; i < 3; i++)
row[i] = map[r][i];
return row;
public String getColumn(int c)
String column = new String[3];
for (int i = 0; i < 3; i++)
column[i] = map[i][c];
return column;
//check win using row & columns method
public boolean checkWinRow()
for (int i = 0; i < 3; i++)
String test = getRow(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinCol()
for (int i = 0; i < 3; i++)
String test = getColumn(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinDiag()
String s = map[1][1];
if (map[0][0].equals(s) && map[2][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else if (map[2][0].equals(s) && map[0][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else
return false;
public boolean checkTie()
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3;a ++)
if (map[i][a].equals(" "))
return false;
return true;
public boolean playerWin = false;
private int turn = 1;
private String map = new String[3][3];
public class AI
public AI(String temp)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
perm[i][a] = temp[i][a];
public boolean compareIt(String check)
boolean notSame = true;
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
if (perm[i][a].equals(check[i][a]))
continue;
else
notSame = false;
return notSame;
return true;
public String retIt()
return perm;
public String perm = new String[3][3];
java performance algorithm tic-tac-toe ai
I designed a tic tac toe using 4 classes. The game class being the main class, the board class being the board, the player class being the decision making, and the AI class keeping the previous game combinations. I found that this code was very inefficient especially as the number of games increases to the hundreds. This game was set specifically for two computers to play against each other. It's not really AI, but it if the same conditions apply (all the other spots are the same), then it won't make the same mistake. What can I do to improve my code efficiency since there are over 250k possible combinations for tic tac toe?
public class Game
public static void main(String args)
int playerWin = 0;
int computerWin = 0;
int ties = 0;
int totalGame = 1;
System.out.println("Welcome to Tic Tac Toe");
Board bgame = new Board();
Player p1 = new Player();
Player p2 = new Player();
bgame.createGrid();
while (true)
bgame.checkWinDiag()) && bgame.playerWin)
System.out.println("You have won!");
bgame.printGrid();
AI lossSave = new AI(bgame.getMap());
p2.addLoss(lossSave);
totalGame++;
playerWin++;
bgame = new Board();
bgame.createGrid();
else if ((bgame.checkWinRow()
import java.io.*;
import java.util.*;
public class Player
public Player()
public int playerMove(String tempMap, int playerTurn)
int ret = new int[2];
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Scanner usr_in = new Scanner(System.in);
String u_in = usr_in.nextLine();
String rAndC = new String[2];
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
while (!(tempMap[ret[1]][ret[0]] == " "))
System.out.println("That spot is already taken, please try again.");
if (playerTurn % 2 == 0)
System.out.println("Enter coordinate to place O, format: 0, 3");
else
System.out.println("Enter coordinate to place X, format: 1, 2");
Board.printGrid(tempMap);
u_in = usr_in.nextLine();
rAndC = u_in.split(", ");
ret[0] = Integer.parseInt(rAndC[0]);
ret[1] = Integer.parseInt(rAndC[1]);
return ret;
public int computerMove(String tempMap, int playerTurn)
String aiCheck = new String[3][3];
String currentPlayer = "";
if (playerTurn % 2 == 0)
currentPlayer = "O";
else
currentPlayer = "X";
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
aiCheck[i][a] = tempMap[i][a];
System.out.println("The computer's turn: ");
int grid = new int[2];
int row;
int column;
do
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
while (tempMap[column][row] != " ");
int aiCounter = -1;
if (rem.size() == 0)
else
aiCheck[row][column] = currentPlayer;
while (aiCounter < rem.size() - 1)
aiCounter++;
if (!((AI)rem.get(aiCounter)).compareIt(aiCheck))
continue;
else
aiCounter = 0;
do
aiCheck[row][column] = tempMap[row][column];
row = (int) (Math.random() * 3);
column = (int) (Math.random() * 3);
aiCheck[row][column] = currentPlayer;
while (tempMap[column][row] != " " && !(((AI)rem.get(aiCounter)).compareIt(aiCheck)));
grid[0] = row;
grid[1] = column;
Board.printGrid(tempMap);
return grid;
public void addLoss(AI a)
rem.add(a);
public List retEv()
return rem;
public List rem = new ArrayList();
public class Board
// Board Size is 3 x 3
public Board()
public void createGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
map[r][c] = " ";
public void setBoard(String test)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
map[i][a] = test[i][a];
// Method to Print Graphics
public void printGrid()
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
c == 1)
");
if (r == 0
System.out.println();
public static void printGrid(String tGrid)
for (int r = 0; r < 3; r++)
for (int c = 0; c < 3; c++)
System.out.print(tGrid[r][c]);
if (c == 0
if (r == 0
System.out.println();
// Method to set a position to a X or O
public void setPosGrid(int rAndC)
int row = rAndC[1];
int column = rAndC[0];
if (turn %2 == 0)
map[row][column] = " O ";
else
map[row][column] = " X ";
turn++;
public void turnNum()
if (turn % 2 == 0)
System.out.println("It is the computer's turn, O");
else
System.out.println("It is the player's turn, X");
public String getMap()
return map;
public int getNum()
return turn;
public String getRow(int r)
String row = new String[3];
for (int i = 0; i < 3; i++)
row[i] = map[r][i];
return row;
public String getColumn(int c)
String column = new String[3];
for (int i = 0; i < 3; i++)
column[i] = map[i][c];
return column;
//check win using row & columns method
public boolean checkWinRow()
for (int i = 0; i < 3; i++)
String test = getRow(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinCol()
for (int i = 0; i < 3; i++)
String test = getColumn(i);
String prev = test[0];
int counter = 0;
for (int a = 1; a < 3; a++)
if (test[a].equals(prev) && prev != " ")
counter++;
else
counter = 0;
if (counter == 2)
if(prev.equals(" X "))
playerWin = true;
return true;
return false;
public boolean checkWinDiag()
String s = map[1][1];
if (map[0][0].equals(s) && map[2][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else if (map[2][0].equals(s) && map[0][2].equals(s) && !(s.equals(" ")))
if (s.equals(" X "))
playerWin = true;
return true;
else
return false;
public boolean checkTie()
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3;a ++)
if (map[i][a].equals(" "))
return false;
return true;
public boolean playerWin = false;
private int turn = 1;
private String map = new String[3][3];
public class AI
public AI(String temp)
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
perm[i][a] = temp[i][a];
public boolean compareIt(String check)
boolean notSame = true;
for (int i = 0; i < 3; i++)
for (int a = 0; a < 3; a++)
if (perm[i][a].equals(check[i][a]))
continue;
else
notSame = false;
return notSame;
return true;
public String retIt()
return perm;
public String perm = new String[3][3];
java performance algorithm tic-tac-toe ai
edited May 21 at 13:18
200_success
123k14143399
123k14143399
asked May 21 at 10:51
XTImpossible
1612
1612
There areimport
statements in the middle of the code, which makes me wonder of you copied multiple source files into a single code box. Also the indentation is very strange.
â Martin R
May 21 at 11:31
1
Every time you lose a game you're adding a new object to a list. That list will get bigger and bigger, so walking through it is going to take more and more time. You should consider a rules-based AI instead.
â Eric Stein
May 21 at 14:47
Can you elaborate on a rules-based AI? The list will become too big and become inefficient. I'm not sure how I would implement a rule based AI to work with the code. My indentation is weird sometimes, I'm not sure why. I believe it's because I didn't split the code into separate classes correctly.
â XTImpossible
May 23 at 22:57
add a comment |Â
There areimport
statements in the middle of the code, which makes me wonder of you copied multiple source files into a single code box. Also the indentation is very strange.
â Martin R
May 21 at 11:31
1
Every time you lose a game you're adding a new object to a list. That list will get bigger and bigger, so walking through it is going to take more and more time. You should consider a rules-based AI instead.
â Eric Stein
May 21 at 14:47
Can you elaborate on a rules-based AI? The list will become too big and become inefficient. I'm not sure how I would implement a rule based AI to work with the code. My indentation is weird sometimes, I'm not sure why. I believe it's because I didn't split the code into separate classes correctly.
â XTImpossible
May 23 at 22:57
There are
import
statements in the middle of the code, which makes me wonder of you copied multiple source files into a single code box. Also the indentation is very strange.â Martin R
May 21 at 11:31
There are
import
statements in the middle of the code, which makes me wonder of you copied multiple source files into a single code box. Also the indentation is very strange.â Martin R
May 21 at 11:31
1
1
Every time you lose a game you're adding a new object to a list. That list will get bigger and bigger, so walking through it is going to take more and more time. You should consider a rules-based AI instead.
â Eric Stein
May 21 at 14:47
Every time you lose a game you're adding a new object to a list. That list will get bigger and bigger, so walking through it is going to take more and more time. You should consider a rules-based AI instead.
â Eric Stein
May 21 at 14:47
Can you elaborate on a rules-based AI? The list will become too big and become inefficient. I'm not sure how I would implement a rule based AI to work with the code. My indentation is weird sometimes, I'm not sure why. I believe it's because I didn't split the code into separate classes correctly.
â XTImpossible
May 23 at 22:57
Can you elaborate on a rules-based AI? The list will become too big and become inefficient. I'm not sure how I would implement a rule based AI to work with the code. My indentation is weird sometimes, I'm not sure why. I believe it's because I didn't split the code into separate classes correctly.
â XTImpossible
May 23 at 22:57
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194861%2ftic-tac-toe-game-becomes-more-inefficient-as-time-goes-on%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
There are
import
statements in the middle of the code, which makes me wonder of you copied multiple source files into a single code box. Also the indentation is very strange.â Martin R
May 21 at 11:31
1
Every time you lose a game you're adding a new object to a list. That list will get bigger and bigger, so walking through it is going to take more and more time. You should consider a rules-based AI instead.
â Eric Stein
May 21 at 14:47
Can you elaborate on a rules-based AI? The list will become too big and become inefficient. I'm not sure how I would implement a rule based AI to work with the code. My indentation is weird sometimes, I'm not sure why. I believe it's because I didn't split the code into separate classes correctly.
â XTImpossible
May 23 at 22:57