Skip to content

Instantly share code, notes, and snippets.

@Gzoref
Created October 3, 2018 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gzoref/3a026aad517e3ba9b3ed025e0b52e0cd to your computer and use it in GitHub Desktop.
Save Gzoref/3a026aad517e3ba9b3ed025e0b52e0cd to your computer and use it in GitHub Desktop.
Tic Tac Toe game
package ticTacToe;
import java.util.Arrays;
import java.util.Scanner;
/*
* Name: Geoffrey Zoref
* Date: 10/3/2018
* Project: Tic Tac Toe
*/
public class GameBoard {
private char[][] gameBoard;
private boolean gameOnGoing = true;
public GameBoard() {
gameBoard = new char[3][3];
for (int row = 0; row < gameBoard.length; row++) {
Arrays.fill(gameBoard[row], ' ');
}
}
//This method will display game board
public boolean gameActive() {
return gameOnGoing;
}
//Ask user to pick row and column.
public void askPlayer(char player) {
Scanner input = new Scanner(System.in);
int row, col;
do {
System.out.printf("Player %s Please enter a row (1-3): ", player);
row = input.nextInt();
System.out.printf("PLayer %s Please enter a column (1-3): ", player);
col = input.nextInt();
} while (notValid(row, col));
makeMove(player, row - 1, col - 1);
}
public boolean notValid(int row, int col) {
if(row > 3 || row < 1 || col > 3 || col < 1 || !isEmpty(row, col))
return true;
else
return false;
}
private boolean isEmpty(int row, int col) {
if(gameBoard[row - 1][col - 1] == ' ')
return true;
else
return false;
}
public void displayBoard() {
for (int row = 0; row < gameBoard.length; row++) {
for (int col = 0; col < gameBoard[0].length; col++) {
System.out.print("\t" + gameBoard[row][col]);
if(col == 0 || col == 1)
System.out.print(" | ");
}
if(row == 0 || row == 1)
System.out.print("\n---------------------\n");
}
System.out.println();
}
//Method checks spaces on game board. If win, return true.
public boolean makeMove(char player, int row, int col) {
if(row >= 0 && row <= 2 && col >= 0 && col <= 2) {
if(gameBoard[row][col] != ' ') {
return false;
}else {
gameBoard[row][col] = player;
return true;
}
}else
return false;
}
public boolean checkForWinner() {
for (int row = 0; row < gameBoard.length; row++) {
if(gameBoard[row][0] == gameBoard[row][1] && gameBoard[row][1] == gameBoard[row][2] && gameBoard[row][0] != ' ') {
System.out.print("Winner is " + gameBoard[row][0]);
gameOnGoing = false;
}
}
for (int col = 0; col < gameBoard[0].length; col++) {
if(gameBoard[0][col] == gameBoard[1][col] && gameBoard[1][col] == gameBoard[2][col] && gameBoard[0][col] != ' ') {
System.out.print("Winner is " + gameBoard[0][col]);
gameOnGoing = false;
}
}
//Check the diagonals
if(gameBoard[0][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[2][2] && gameBoard[0][0] != ' ') {
System.out.print("Winner is " + gameBoard[0][0]);
gameOnGoing = false;
}
if(gameBoard[2][0] == gameBoard[1][1] && gameBoard[1][1] == gameBoard[0][2] && gameBoard[0][2] != ' ') {
System.out.print("Winner is " + gameBoard[1][1]);
gameOnGoing = false;
}
return false;
}
}
/**
*/
package ticTacToe;
import ticTacToe.GameBoard;
/*
* Name: Geoffrey Zoref
* Date: 10/3/2018
* Project: Tic Tac Toe
*/
public class Main {
public static void main(String[] args) {
GameBoard game = new GameBoard();
game.displayBoard();
int counter = 1;
while (game.gameActive() && counter < 10) {
if(counter % 2 == 0)
game.askPlayer('O');
else
game.askPlayer('X');
counter++;
System.out.println("\n");
game.displayBoard();
game.checkForWinner();
if(counter == 10){
System.out.print("Stale mate!\n");
}
}
}
}
/**
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment