Skip to content

Instantly share code, notes, and snippets.

@molayodecker
Last active December 10, 2016 13:07
Show Gist options
  • Save molayodecker/e7d1250f187f31c107fa6b637666cfb2 to your computer and use it in GitHub Desktop.
Save molayodecker/e7d1250f187f31c107fa6b637666cfb2 to your computer and use it in GitHub Desktop.
Recurse Pair Interview Programming tasks

Tic Tac Toe game

Playground

This project contains two files, Game.java and playGround.java The main class with public static void main function is Game.java and the subclass which contains and Constructor all the methods is playGround.java


This Game has been developed to use the Characters 'O' and 'X'to represent players so users need not create players. Also humans count from 1 so i have created the game for players to move from 1 - 3 eventhough machines and computers count from zero (0). Hence the first move to the first position of the two-dimensional array will be [0][0] but the user will have to enter [1][1] because humans count from 1.


As a player i should be presented with a menu to start the Game, so that i can make my first move.


As a player i should be so be able to have turns in the game, so that my opponent can also make moves.


As a player i should be able to know when the Game has come to an end, so that i can know if the game was a draw or a win.

/**
* Created by molayodecker on 28/11/2016.
* Welcome
* Before your interview, write a program that lets two humans play a game of Tic Tac Toe in a terminal.
* The program should let the players take turns to input their moves.
* The program should report the outcome of the game.
* During your interview, you will pair on adding support for a computer player to your game.
* You can start with random moves and make the AI smarter if you have time.
*
* Game : Tic Tac Toe game
* | |
* -----|-----|-----
* | |
* -----|-----|-----
* | |
*
*
*/
public class Game {
public static void main(String[] args) {
playGround playground = new playGround();
System.out.println("Welcome to the Tic Tac Toe game");
System.out.println("Please follow the Menu to start the Game\n");
playground.displayPlayGround();
System.out.println("\n");
int counter = 1;
while (playground.gameActive() && counter < 10){
if(counter % 2 == 0){
playground.askPlayer('O');
}else {
playground.askPlayer('X');
}
counter++;
playground.displayPlayGround();
System.out.println("\n");
playground.checkWinner();
System.out.println("\n");
if(counter == 10){
System.out.println("We have a draw!");
}
}
}
}
import java.util.Arrays;
import java.util.Scanner;
/**
* Created by molayodecker on 28/11/2016.
* Welcome
* Before your interview, write a program that lets two humans play a game of Tic Tac Toe in a terminal.
* The program should let the players take turns to input their moves.
* The program should report the outcome of the game.
* During your interview, you will pair on adding support for a computer player to your game.
* You can start with random moves and make the AI smarter if you have time.
*
* Game : Tic Tac Toe game
* | |
* -----|-----|-----
* | |
* -----|-----|-----
* | |
*
*
*/
public class playGround {
private char[][] playground;
private boolean gameOngoing = true;
public playGround() {
this.playground = new char[3][3];
for (int row = 0; row < playground.length; row++){
Arrays.fill(playground[row], ' ');
}
}
/*
* This method just displays the tictactoe playground
* */
public void displayPlayGround(){
for (int row = 0; row < playground.length; row++){
for(int col = 0; col < playground[0].length; col++){
System.out.print("\t" +playground[row][col]);
if(col == 0 || col == 1){
System.out.print("\t|");
}
}
if(row == 0 || row == 1) {
System.out.printf("%n-------------------------%n");
}
}
}
/*
This method checks if the row or col the player is trying to play is empty or filled.
if it's not empty return false else add the player in that position
* */
public boolean play(char player, int row, int col){
if(row >= 0 && row <= 2 && col >= 0 && col <= 2){
if(playground[row][col] != ' '){
return false;
}else {
playground[row][col] = player;
return true;
}
}
return false;
}// end of play
/*This method checks if the game is ongoing or still active
The default value for gameOngoing is true
So this method sets the gameActive method to true
* */
public boolean gameActive(){
return gameOngoing;
}
/*
* This method ask the player to enter the rows and columns he want to play.
* If the move is valid (Meaning if it has not been played) it plays that position
* */
public void askPlayer(char player){
Scanner stdin = new Scanner(System.in);
int row,col;
do{
System.out.printf("Player %s, please enter a row (1 - 3): ", player);
row = stdin.nextInt();
System.out.printf("Player %s, Please enter a column (1 - 3): ", player);
col = stdin.nextInt();
}while (inValid(row,col));
play(player,row-1,col-1);
}
/* Invalid method checks to see if the move is between 1 - 3 and not empty.
If the move is valid return true if not return false
* */
public boolean inValid(int row, int col){
if(row > 3 || row < 1 || col > 3 || col < 1 || !isEmpty(row,col)){
return true;
}else{
return false;
}
}
public boolean isEmpty(int row, int col){
if(playground[row-1][col-1] == ' '){
return true;
}else {
System.out.println("Wrong move");
return false;
}
}
/*
The checkwinner() method checks to see if 'X' or 'O' has been able to form a straight horizontal, Vertical
or diagonal pattern.
* */
public boolean checkWinner(){
for(int row = 0; row < playground.length; row++){
if(playground[row][0] == playground[row][1] && playground[row][1] == playground[row][2] && playground[row][0] != ' '){
System.out.println("The winner is " + playground[row][0]);
gameOngoing = false;
}
}
for(int col = 0; col < playground.length; col++){
if(playground[0][col] == playground[1][col] && playground[1][col] == playground[2][col] && playground[0][col] != ' '){
System.out.println("The winner is " + playground[0][col]);
gameOngoing = false;
}
}
if(playground[0][0] == playground[1][1] && playground[1][1] == playground[2][2] && playground[0][0] != ' '){
System.out.println("The winner is " + playground[0][0]);
gameOngoing = false;
}
if(playground[0][2] == playground[1][1] && playground[1][1] == playground[2][0] && playground[0][2] != ' '){
System.out.println("The winner is " + playground[0][2]);
gameOngoing = false;
}
return false;
}
}
@molayodecker
Copy link
Author

copy of abstract

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment