Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- //bugs - who won, extra draw, because place method should print then check winner
- import javax.swing.*;
- public class TicTacToe
- {
- static Random gen = new Random();
- static int[] freeSpots;
- static String rowParts;
- static int position;
- static boolean over = false;
- static boolean[] occupation = new boolean[9];
- static String header = "Choose a number 1-9.";
- static char playerChar, aiChar;
- static String[] options = {"X", "O"};
- static int[][] winnings = {{0,0,0,2,0,0,0,4},
- {2,0,2,2,2,0,2,4},
- {4,0,4,2,4,0,4,4},
- {0,0,2,0,0,0,4,0},
- {0,2,2,2,0,2,4,2},
- {0,4,2,4,0,4,4,4},
- {0,0,2,2,0,0,4,4},
- {0,4,2,2,0,4,4,0}};
- static char[][] board =
- {{' ', '|', ' ', '|', ' '},
- {'-', '+', '-', '+', '-'},
- {' ', '|', ' ', '|', ' '},
- {'-', '+', '-', '+', '-'},
- {' ', '|', ' ', '|', ' '}};
- public static void main(String[] args)
- {
- printGrid(board);
- initialize();
- do
- {
- do
- {
- position = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a number!", header, JOptionPane.INFORMATION_MESSAGE));
- if (occupation[position-1] == true)
- {
- header = "This spot is occupied!";
- }
- else header = "Pick a number";
- } while(occupation[position-1] != false);
- place(board, true, position, true);
- if (over == false)
- {
- aiMove();
- checkWinner();
- }
- } while (over != true);
- }
- public static void initialize()
- {
- switch (JOptionPane.showOptionDialog(null, "Pick a letter!", "TicTacToe", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, options, options[0]))
- {
- case 0:
- {
- playerChar = 'X';
- aiChar = 'O';
- break;
- }
- case 1:
- {
- playerChar = 'O';
- aiChar = 'X';
- break;
- }
- }
- for (int i = 0; i<occupation.length; i++)
- {
- occupation[i] = false;
- }
- }
- public static void place(char[][] gameBoard, boolean player, int pos, boolean truePlacement)
- {
- if (player == true)
- gameBoard[2*((int)(pos*.33))][(int)(Double.parseDouble(((((Double.toString((pos%3)-1)))).replace("-1", "2")))*2)] = playerChar;
- else gameBoard[2*((int)(pos*.33))][(int)(Double.parseDouble(((((Double.toString((pos%3)-1)))).replace("-1", "2")))*2)] = aiChar;
- if (truePlacement == true)
- {
- occupation[pos-1] = true;
- if (player == false) printGrid(board);
- }
- }
- public static void printGrid(char[][] grid)
- {
- clear();
- for (char[] row : grid)
- {
- rowParts="";
- for (char c : row)
- rowParts+=c;
- System.out.println(rowParts);
- }
- }
- public static void aiMove()
- {
- //make array of available spots. see if putting a point in each spot will make you win, or make them win. if so, place there with intelligence percent
- freeSpots = null;
- freeSpots = new int[numFreeSpots()];
- int freeSpotsFound = 0;
- for (int i=0; i<9; i++)
- {
- if (occupation[i] == false)
- {
- freeSpots[freeSpotsFound] = i+1;
- freeSpotsFound++;
- }
- }
- char[][] testBoard = board.clone();
- testBoard = board; //Copy the board to a new variable to test winning
- for (int spot: freeSpots)
- {
- place(testBoard, false, spot, false);
- for (int i = 0; i<8; i++)
- {
- if (testBoard[winnings[i][0]][winnings[i][1]] == testBoard[winnings[i][2]][winnings[i][3]] && testBoard[winnings[i][4]][winnings[i][5]] == testBoard[winnings[i][6]][winnings[i][7]])
- {
- if (testBoard[winnings[i][0]][winnings[i][1]] != ' ' && testBoard[winnings[i][2]][winnings[i][3]] != ' ' && testBoard[winnings[i][6]][winnings[i][7]] != ' ')
- {
- place(board, false, spot, true);
- return;
- }
- }
- }
- testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
- }
- //At this point, no game-winning spots were found. Next, we will try to avoid a loss.
- for (int spot: freeSpots)
- {
- place(testBoard, true, spot, false);
- for (int i = 0; i<8; i++)
- {
- if (testBoard[winnings[i][0]][winnings[i][1]] == testBoard[winnings[i][2]][winnings[i][3]] && testBoard[winnings[i][4]][winnings[i][5]] == testBoard[winnings[i][6]][winnings[i][7]])
- {
- if (testBoard[winnings[i][0]][winnings[i][1]] != ' ' && testBoard[winnings[i][2]][winnings[i][3]] != ' ' && testBoard[winnings[i][6]][winnings[i][7]] != ' ')
- {
- place(board, false, spot, true);
- return;
- }
- }
- }
- testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
- }
- //At this point, we couldn't win the game OR avoid a loss. Now we will look into the future and plan a path by placing TWO fake points in available locations.
- //If this path can cause us to win, pick one of the potential spots at random, so that we don't just draw a straight line and reveal our plan.
- for (int j = 0; j<numFreeSpots()*10; j++)
- {
- freeSpots = null;
- freeSpots = new int[numFreeSpots()];
- freeSpotsFound = 0;
- for (int i=0; i<9; i++)
- {
- if (occupation[i] == false)
- {
- freeSpots[freeSpotsFound] = i+1;
- freeSpotsFound++;
- }
- }
- int index = 0;
- int[] freeSpotsCopy = freeSpots.clone();
- int spot = freeSpotsCopy[gen.nextInt(freeSpotsCopy.length)];
- freeSpotsCopy = null;
- freeSpotsCopy = new int[freeSpots.length-1];
- for (int s: freeSpots)
- {
- if (s != spot)
- {
- freeSpotsCopy[index] = s;
- index++;
- }
- }
- int spot2 = freeSpotsCopy[gen.nextInt(freeSpotsCopy.length)];
- place(testBoard, false, spot, false);
- place(testBoard, false, spot2, false);
- for (int i = 0; i<8; i++)
- {
- if (testBoard[winnings[i][0]][winnings[i][1]] == testBoard[winnings[i][2]][winnings[i][3]] && testBoard[winnings[i][4]][winnings[i][5]] == testBoard[winnings[i][6]][winnings[i][7]])
- {
- if (testBoard[winnings[i][0]][winnings[i][1]] != ' ' && testBoard[winnings[i][2]][winnings[i][3]] != ' ' && testBoard[winnings[i][6]][winnings[i][7]] != ' ')
- {
- testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
- testBoard[2*((int)(spot2*.33))][(int)(Double.parseDouble(((((Double.toString((spot2%3)-1)))).replace("-1", "2")))*2)] = ' ';
- place(board, false, spot, true);
- return;
- }
- }
- }
- testBoard[2*((int)(spot*.33))][(int)(Double.parseDouble(((((Double.toString((spot%3)-1)))).replace("-1", "2")))*2)] = ' ';
- testBoard[2*((int)(spot2*.33))][(int)(Double.parseDouble(((((Double.toString((spot2%3)-1)))).replace("-1", "2")))*2)] = ' ';
- }
- //At this point, we couldn't win, block, or find a winning path. So now, we will pick a point at random and go there.
- freeSpots = null;
- freeSpots = new int[numFreeSpots()];
- freeSpotsFound = 0;
- for (int i=0; i<9; i++)
- {
- if (occupation[i] == false)
- {
- //System.out.println("found a free spot at "+(i+1));
- freeSpots[freeSpotsFound] = i+1;
- freeSpotsFound++;
- }
- }
- //System.out.println(freeSpots.length);
- place(board, false, freeSpots[gen.nextInt(freeSpots.length)], true);
- return;
- }
- public static void checkWinner()
- {
- for (int i = 0; i<8; i++)
- {
- if (board[winnings[i][0]][winnings[i][1]] == board[winnings[i][2]][winnings[i][3]] && board[winnings[i][4]][winnings[i][5]] == board[winnings[i][6]][winnings[i][7]])
- {
- if (board[winnings[i][0]][winnings[i][1]] != ' ' && board[winnings[i][2]][winnings[i][3]] != ' ' && board[winnings[i][6]][winnings[i][7]] != ' ')
- {
- printGrid(board);
- over = true;
- if (boardFull() == true)
- {
- JOptionPane.showMessageDialog(null, "Tie!");
- }
- else if (board[winnings[i][0]][winnings[0][i]] == playerChar)
- JOptionPane.showMessageDialog(null, "You win!");
- else JOptionPane.showMessageDialog(null, "You lose!");
- }
- }
- }
- }
- public static int numFreeSpots()
- {
- int spots = 0;
- for (boolean b: occupation)
- {
- if (b == false)
- {
- spots++;
- }
- }
- return spots;
- }
- public static boolean boardFull()
- {
- for (boolean b: occupation)
- {
- if (b == false)
- {
- return false;
- }
- }
- return true;
- }
- public static void clear()
- {
- for (int i = 0; i<=10; i++)
- {
- System.out.println(" ");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement