Skip to content

Instantly share code, notes, and snippets.

@ataipale
Created February 3, 2015 18:10
Show Gist options
  • Save ataipale/22d7f1b08c921048d703 to your computer and use it in GitHub Desktop.
Save ataipale/22d7f1b08c921048d703 to your computer and use it in GitHub Desktop.
My TicTacToe game for two humans!
/*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
int moves = 0;
boolean hasWon = false;
while(!hasWon && moves < 9){
//to check that entered coordinates were acceptable
boolean successfulAdd = false;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Player " + newGame.getNextPlayer() + " Turn.");
System.out.println();
System.out.println("Current Board: ");
newGame.printBoard();
while (!successfulAdd) {
System.out.println();
System.out.println("Please enter your desired row number: ");
int row = Integer.parseInt(br.readLine());
System.out.println("Please enter your desired column number: ");
int column = Integer.parseInt(br.readLine());
//Checks for in-bound coordinates
if(row < 1 || row > 3 || column < 1 || column > 3) {
System.out.println("Coordinates out of bounds. Please enter new coordinates.");
} else {
//Checks for open spot
if(newGame.getChar(row, column) == '-'){
newGame.addToBoard(row, column);
successfulAdd = true;
}else {
System.out.println("Spot already taken! Please enter new coordinates.");
}
}
}
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 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(){
//only need to check 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
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment