Created
February 4, 2015 00:40
-
-
Save ataipale/d819fd03a32cb77a17e9 to your computer and use it in GitHub Desktop.
Tic Tac Toe for Two Humans
This file contains hidden or 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
/*Alex Taipale, 2014.02.02 | |
* Tic Tac Toe Game to be played between two humans in the console. | |
*/ | |
import java.io.BufferedReader; | |
import java.io.IOException; | |
import java.io.InputStreamReader; | |
public class TicTacToeGame { | |
public static void main(String[] args) throws NumberFormatException, IOException { | |
TicTacToe newGame = new TicTacToe(); | |
//track number of moves that have been made, to preclude checking for | |
//winning when less than 3 moves have been made by one player and to | |
//stop the game if 9 moves have been made, because the gris will be full! | |
int moves = 0; | |
boolean hasWon = false; | |
while(!hasWon && moves < 9){ | |
newGame.getNextPlayer(); | |
System.out.println(); | |
System.out.println("Current Board: "); | |
newGame.printBoard(); | |
int[] play = getPlayerMove(newGame); | |
newGame.addToBoard(play[0], play[1]); | |
moves++; | |
if (moves > 4) { | |
hasWon = newGame.checkforwin(); | |
} | |
} | |
System.out.println("End Board: "); | |
newGame.printBoard(); | |
System.out.println(); | |
if (hasWon) { | |
System.out.println("Congrats, Player " + newGame.getCurrentPlayer() + ", you won!"); | |
} else { | |
System.out.println("It's a draw!"); | |
} | |
} | |
public static int[] getPlayerMove(TicTacToe Game) throws NumberFormatException, IOException { | |
int[] playerMove = new int[2]; | |
//to check that entered coordinates were acceptable | |
boolean successfulAdd = false; | |
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("Human Player " + Game.getCurrentPlayer() + " Turn."); | |
System.out.println(); | |
while (!successfulAdd) { | |
System.out.println(); | |
System.out.print("Please enter your desired row number: "); | |
playerMove[0] = Integer.parseInt(br.readLine()); | |
System.out.print("Please enter your desired column number: "); | |
playerMove[1] = Integer.parseInt(br.readLine()); | |
//Checks for in-bound coordinates | |
if(playerMove[0] < 1 || playerMove[0] > 3 || playerMove[1] < 1 || playerMove[1] > 3) { | |
System.out.println("Coordinates out of bounds. Please enter new coordinates."); | |
} else { | |
//Checks for open spot | |
if(Game.getChar(playerMove[0], playerMove[1]) == '-'){ | |
successfulAdd = true; | |
}else { | |
System.out.println("Spot already taken! Please enter new coordinates."); | |
} | |
} | |
} | |
return playerMove; | |
} | |
} | |
public class TicTacToe { | |
char[][] board = new char[3][3]; | |
//o will start the game (because next player changes player at start of game) | |
char currentPlayer = 'x'; | |
public TicTacToe(){ | |
newBoard(); | |
} | |
public void newBoard() { | |
for(int i = 0; i < 3; i++){ | |
//Print out each row first | |
for(int j = 0; j < 3; j++) { | |
board[i][j] = '-'; | |
} | |
//Skip a line, print the next row | |
System.out.println(); | |
} | |
} | |
public void printBoard() { | |
for(int i = 0; i < 3; i++){ | |
//Print out each row first | |
for(int j = 0; j < 3; j++) { | |
System.out.print(board[i][j] + " "); | |
} | |
//Skip a line, print the next row | |
System.out.println(); | |
} | |
} | |
// check if the game has been won | |
public boolean checkforwin(){ | |
/*In the future: | |
Maybe I can first check the 3 diagonal squares for player, | |
because it is not possible to have 3 in a row w/o using one | |
of those squares. */ | |
//check rows | |
for(int i = 0; i < 3; i++){ | |
for(int j = 0; j < 3; j++) { | |
if(board[i][j] == currentPlayer) { | |
if(j == 2) { | |
return true; | |
} | |
} else { | |
//exit the j loop (I know this looks stupid... trying to | |
//think of a better way...) :) | |
j = 3; | |
} | |
} | |
} | |
//Check Columns | |
for(int j = 0; j < 3; j++){ | |
for(int i = 0; i < 3; i++) { | |
if(board[i][j] == currentPlayer) { | |
if(i == 2) { | |
return true; | |
} | |
} else { | |
//exit the i loop | |
i = 3; | |
} | |
} | |
} | |
//Check Diagonals | |
if(board[1][1] == currentPlayer) { | |
if(board[0][0] == currentPlayer && board[2][2] == currentPlayer){ | |
return true; | |
} if(board[0][2] == currentPlayer && board[2][0] == currentPlayer){ | |
return true; | |
} | |
} | |
return false; | |
} | |
public char getChar(int x, int y) { | |
return board[x-1][y-1]; | |
} | |
public char getCurrentPlayer(){ | |
return currentPlayer; | |
} | |
public char getNextPlayer(){ | |
if(currentPlayer == 'x'){ | |
currentPlayer = 'o'; | |
} else { | |
currentPlayer = 'x'; | |
} | |
return currentPlayer; | |
} | |
public void addToBoard(int x, int y) { | |
board[x-1][y-1] = getCurrentPlayer(); | |
} | |
public char[][] getBoard() { | |
return board; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment