Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package gameZone;
- import javax.swing.JOptionPane;
- public class TicTacToe {
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- char ttt[][] = {{'0','1','2',
- '3','4','5',
- '6','7','8'}};
- int uC;
- boolean win = false;
- boolean lose = false;
- int b = 0;
- int compChoice = 100;
- int end = 0;
- while(win == false && lose == false && b <= 9 && end != 999)
- {
- do
- {
- compChoice = (int)(Math.random() * 9);
- }
- while(ttt[0][compChoice] == 'O' || ttt[0][compChoice] == 'X');
- uC = Integer.parseInt(JOptionPane.showInputDialog(null, ttt[0][0] + "," + ttt[0][1] + "," + ttt[0][2] + "\n" +
- ttt[0][3] + "," + ttt[0][4] + "," + ttt[0][5] + "\n" +
- ttt[0][6] + "," + ttt[0][7] + "," + ttt[0][8] + "\nPlease enter "
- + "a number where you want to place an X."));
- b = b + 1;
- if(b <= 8)
- {
- compChoice = (int)(Math.random() * 9);
- b = b + 1;
- }
- while(ttt[0][uC] == 'X' || ttt[0][uC] == 'O')
- {
- uC = Integer.parseInt(JOptionPane.showInputDialog(null, ttt[0][0] + "," + ttt[0][1] + "," + ttt[0][2] + "\n" +
- ttt[0][3] + "," + ttt[0][4] + "," + ttt[0][5] + "\n" +
- ttt[0][6] + "," + ttt[0][7] + "," + ttt[0][8] + "\nPlease choose another location"));
- }
- ttt[0][uC] = 'X';
- ttt[0][compChoice] = 'O';
- if(ttt[0][0] == 'X' && ttt[0][1] =='X' && ttt[0][2] == 'X' ||
- ttt[0][3] == 'X' && ttt[0][4] =='X' && ttt[0][5] == 'X' ||
- ttt[0][6] == 'X' && ttt[0][7] =='X' && ttt[0][8] == 'X' ||
- ttt[0][0] == 'X' && ttt[0][3] =='X' && ttt[0][6] == 'X' ||
- ttt[0][1] == 'X' && ttt[0][4] =='X' && ttt[0][7] == 'X' ||
- ttt[0][2] == 'X' && ttt[0][5] =='X' && ttt[0][8] == 'X' ||
- ttt[0][0] == 'X' && ttt[0][4] =='X' && ttt[0][8] == 'X' ||
- ttt[0][2] == 'X' && ttt[0][4] =='X' && ttt[0][6] == 'X')
- {
- win = true;
- JOptionPane.showMessageDialog(null, ttt[0][0] + "," + ttt[0][1] + "," + ttt[0][2] + "\n" +
- ttt[0][3] + "," + ttt[0][4] + "," + ttt[0][5] + "\n" +
- ttt[0][6] + "," + ttt[0][7] + "," + ttt[0][8] + "\nYou Win!");
- }
- if(ttt[0][0] == 'O' && ttt[0][1] =='O' && ttt[0][2] == 'O' ||
- ttt[0][3] == 'O' && ttt[0][4] =='O' && ttt[0][5] == 'O' ||
- ttt[0][6] == 'O' && ttt[0][7] =='O' && ttt[0][8] == 'O' ||
- ttt[0][0] == 'O' && ttt[0][3] =='O' && ttt[0][6] == 'O' ||
- ttt[0][1] == 'O' && ttt[0][4] =='O' && ttt[0][7] == 'O' ||
- ttt[0][2] == 'O' && ttt[0][5] =='O' && ttt[0][8] == 'O' ||
- ttt[0][0] == 'O' && ttt[0][4] =='O' && ttt[0][8] == 'O' ||
- ttt[0][2] == 'O' && ttt[0][4] =='O' && ttt[0][6] == 'O')
- {
- lose = true;
- JOptionPane.showMessageDialog(null, ttt[0][0] + "," + ttt[0][1] + "," + ttt[0][2] + "\n" +
- ttt[0][3] + "," + ttt[0][4] + "," + ttt[0][5] + "\n" +
- ttt[0][6] + "," + ttt[0][7] + "," + ttt[0][8] + "\nYou lose. :(");
- }
- if(b == 9 && win == false && lose == false)
- {
- JOptionPane.showMessageDialog(null, ttt[0][0] + "," + ttt[0][1] + "," + ttt[0][2] + "\n" +
- ttt[0][3] + "," + ttt[0][4] + "," + ttt[0][5] + "\n" +
- ttt[0][6] + "," + ttt[0][7] + "," + ttt[0][8] + "\nYou tied. :/");
- end = 999;
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment