Skip to content

Instantly share code, notes, and snippets.

@guillermo-menjivar
Last active April 9, 2018 05:22
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 guillermo-menjivar/38a6f6ac68543552ccd724961751825b to your computer and use it in GitHub Desktop.
Save guillermo-menjivar/38a6f6ac68543552ccd724961751825b to your computer and use it in GitHub Desktop.
example for ya
import java.util.*;
public class GTac {
public static String PlayerO = " O ";
public static String PlayerX = " X ";
public static String Blank = " ";
public static String Delineator = "-----------"; // 11 ( each entry has 3 spaces and there are 3 entries + 2 deviders = 11
public static String Devider = "|";
public static void main(String[] args){
int[][] board = new int[3][3];
board[0][0] = 1;
board[0][1] = 0;
board[0][2] = 2;
board[1][0] = 2;
board[1][1] = 1;
board[1][2] = 0;
board[2][0] = 0;
board[2][1] = 0;
board[2][2] = 1;
System.out.println("Human, play!");
printBoard(board);
int Winner = determineWinner(board);
boolean gameoverBoardfull = GameOver(board);
if (Winner == 1){
System.out.println("Winner");
System.out.println(PlayerX);
} else if (Winner == 2){
System.out.println("Winner");
System.out.println(PlayerO);
} else if (Winner == 0 && gameoverBoardfull == true) {
System.out.println("DRAW!!");
}
}
public static boolean GameOver(int[][] board){
boolean completed = true; // we will change this when we find a zero which means there are still spaces available.
for (int row=0; row < board.length; row++)
{
for (int col=0; col < board.length; col++)
{
if (board[row][col] == 0){
completed = false;
break; // we will get out of the loop because we found an empty space 0 equals empty space on the board
}
}
}
return completed;
}
public static void printBoard(int[][] board){
for (int row=0; row < board.length; row++)
{
for (int col=0; col < board.length; col++)
{
if (col > 0){
System.out.print(Devider);
}
if (board[row][col] == 1){
System.out.print(PlayerX);
} else if (board[row][col] == 2){
System.out.print(PlayerO);
} else {
System.out.print(Blank);
}
if (col == 2){
System.out.println("");
}
}
System.out.println(Delineator);
}
}
public static int determineWinner(int[][]board){
int[]players = new int[2];
int winner = 0; // if winner remains 0 then is a draw
players[0] = 1; // add player one to the array of player to check for winners
players[1] = 2; // add player two to the array of players to check for winners
// check for the winner or draw
for (int p=0; p < players.length; p++)
{
int player = players[p];
if (board[0][0] == player && board[0][1] == player && board[0][2] == player){
winner = player;
}
if (board[1][0] == player && board[1][1] == player && board[1][2] == player){
winner = player;
}
if (board[2][0] == player && board[2][1] == player && board[2][2] == player){
winner = player;
}
//diagonal wins
if (board[0][0] == player && board[1][1] == player && board[2][2] == player){
winner = player;
}
if (board[2][0] == player && board[1][1] == player && board[0][2] == player){
winner = player;
}
}
return winner;
}
}
@guillermo-menjivar
Copy link
Author

guillermo-menjivar commented Apr 9, 2018

Human, play!
 X |   | O 
-----------
 O | X |   
-----------
   |   | X 
-----------

@guillermo-menjivar
Copy link
Author

Human, play!
 X |   | O 
-----------
 O | X |   
-----------
   |   | X 
-----------
Winner
 X

@guillermo-menjivar
Copy link
Author

It now checks if the boards is full which means a proper draw

@guillermo-menjivar
Copy link
Author

ghack$ java GTac                                                                                                                                                                                               
Human, play!
 X | X | O 
-----------
 O | X | X 
-----------
 X | X | X 
-----------
Winner
 X 
                                                                                                                                                                                         
ghack$ javac GTac.java                                                                                                                                                                                         
ghack$ java GTac                                                                                                                                                                                               
Human, play!
 X | X | O 
-----------
 O | O | X 
-----------
 X | X | X 
-----------
Winner
 X 
                                                                                                                                                                                         
ghack$ javac GTac.java                                                                                                                                                                                         
ghack$ java GTac                                                                                                                                                                                               
Human, play!
 X | X | O 
-----------
 O | O | X 
-----------
 X | O | X 
-----------
DRAW!!

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