Skip to content

Instantly share code, notes, and snippets.

@Aleyasen
Created January 26, 2016 03:52
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 Aleyasen/1b6b258ab7de8491fd1b to your computer and use it in GitHub Desktop.
Save Aleyasen/1b6b258ab7de8491fd1b to your computer and use it in GitHub Desktop.
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Amirhossein Aleyasen <aleyase2@illinois.edu>
* created on Jan 25, 2016, 9:38:45 PM
*/
public class TicTacToe {
public static void main(String[] args) {
}
public String ticTacToeCheck(String[][] game) {
for (int i = 0; i < 3; i++) { // checks for rows if we can find any winner
if (game[i][0].equals(game[i][1]) && game[i][0].equals(game[i][2])) { // check if we have a winner in row i
return game[i][0]; // return the winner character
}
}
for (int j = 0; j < 3; j++) { //checkes for columns if we ca find any winner
if (game[0][j].equals(game[1][j]) && game[0][j].equals(game[2][j])) { // check if we have a winner in column j
return game[0][j]; // return the winner character
}
}
if (game[0][0].equals(game[1][1]) && game[0][0].equals(game[2][2])) { //check for main diagonal
return game[0][0]; // return the winner character
}
if (game[0][2].equals(game[1][1]) && game[0][2].equals(game[2][0])) { //check for the second diagonal
return game[0][2]; //return the winner character
}
return "C"; // if the game ended in tie's.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment