Created
March 21, 2013 03:24
-
-
Save shade34321/5210462 to your computer and use it in GitHub Desktop.
Simple tic tac toe game
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
public class TicTacToe { | |
private char board[][]; | |
TicTacToe(){ | |
board = new char[3][3]; | |
for(int i = 0;i<board.length;i++){ | |
for(int j = 0;j<board[i].length;j++){ | |
board[i][j] = '-'; | |
} | |
} | |
} | |
void displayUpdate(){ | |
for(int i = 0;i<board.length;i++){ | |
for(int j = 0;j<board[i].length;j++){ | |
System.out.print(board[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
boolean insertNextMove(char c, int row, int col){ | |
boolean legal = true; | |
try{ | |
if(!(c != 'X' || c!= 'x' || c!='o' || c!='O')){ | |
throw new IllegalArgumentException("Invalid charcter"); | |
} | |
if((row >3 || row < 0 || col > 3 || col < 0)){ | |
throw new ArrayIndexOutOfBoundsException("Move is not within the board parameters."); | |
} | |
if(board[row][col] == 'X' || board[row][col] == 'O'){ | |
throw new IllegalArgumentException("Move is invalid please try again, that space is occupied."); | |
} | |
else{ | |
board[row][col] = c; | |
} | |
} | |
catch(IllegalArgumentException e){ | |
legal = false; | |
System.out.println(e); | |
} | |
catch(ArrayIndexOutOfBoundsException e){ | |
legal = false; | |
System.out.println(e); | |
} | |
return legal; | |
} | |
boolean gameOver(){ | |
boolean winner = false; | |
for(int i = 0;i<board.length;i++){ | |
boolean h = board[i][0] == board[i][1] && board[i][0] == board[i][2]; | |
boolean v = board[0][i] == board[1][i] && board[0][i] == board[2][i]; | |
if(h && board[i][0] != '-'){ | |
System.out.println("Player " + board[i][0] + " wins!"); | |
winner = true; | |
return winner; | |
} | |
if(v && board[0][i] != '-'){ | |
System.out.println("Player " + board[0][i] + " wins!"); | |
winner = true; | |
return winner; | |
} | |
} | |
if(board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != '-'){ | |
System.out.println("Player " + board[0][0] + " wins!"); | |
winner = true; | |
return winner; | |
} | |
if(board[0][2] == board[1][1] && board[0][2] == board[0][2] && board[2][0] != '-'){ | |
System.out.println("Player " + board[2][0] + " wins!"); | |
winner = true; | |
return winner; | |
} | |
return winner; | |
} | |
public static void main(String[] args){ | |
Scanner in = new Scanner(System.in); | |
TicTacToe t = new TicTacToe(); | |
boolean cont = true; | |
while(cont){ | |
System.out.println("Please enter what playing piece you are using, either X or O"); | |
char[] temp = in.next().toCharArray(); | |
char player = temp[0]; | |
System.out.println("Please enter the row you wish to put your piece:"); | |
int row = in.nextInt(); | |
System.out.println("Please enter the column you wish to put your piece:"); | |
int col = in.nextInt(); | |
t.insertNextMove(player, (row-1), (col-1)); | |
t.displayUpdate(); | |
cont = !t.gameOver(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment