Last active
December 16, 2015 16:58
-
-
Save bchetty/5466563 to your computer and use it in GitHub Desktop.
GCJ 2013 - Tic-Tac-Toe-Tomek
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.bchetty.gcj2013; | |
/** | |
* | |
* @author Babji Prashanth, Chetty | |
*/ | |
public class TicTacToe { | |
//Possible 'X' winning combination strings | |
public static final String xWin1 = "XXXX"; | |
public static final String xWin2 = "XXXT"; | |
public static final String xWin3 = "XXTX"; | |
public static final String xWin4 = "XTXX"; | |
public static final String xWin5 = "TXXX"; | |
//Possible 'O' winning combination strings | |
public static final String oWin1 = "OOOO"; | |
public static final String oWin2 = "OOOT"; | |
public static final String oWin3 = "OOTO"; | |
public static final String oWin4 = "OTOO"; | |
public static final String oWin5 = "TOOO"; | |
/** | |
* This method returns the status of the game | |
* | |
* @param board | |
* @return | |
*/ | |
private String getStatusQuo(String[] board) { | |
boolean notComplete = false; | |
//Loop through all rows | |
for(int i=0;i<4;i++) { | |
if(board[i].indexOf(".") < 0) { | |
String res1 = check(board[i]); | |
if(res1 != null) { | |
return res1; | |
} | |
} else { | |
notComplete = true; | |
} | |
//Loop through all columns | |
if(i==0) { | |
for(int j=0;j<4;j++) { | |
String col = board[0].charAt(j) + "" + board[1].charAt(j) + "" + board[2].charAt(j) + "" + | |
board[3].charAt(j); | |
if(col.indexOf(".") < 0) { | |
String res2 = check(col); | |
if(res2 != null) { | |
return res2; | |
} | |
} else { | |
notComplete = true; | |
} | |
//Construct the first diagonal and check it | |
if(j == 0) { | |
String diag1 = board[0].charAt(0) + "" + board[1].charAt(1) + "" + board[2].charAt(2) + "" + | |
board[3].charAt(3); | |
if(diag1.indexOf(".") < 0) { | |
String res3 = check(diag1); | |
if(res3 != null) { | |
return res3; | |
} | |
} else { | |
notComplete = true; | |
} | |
} | |
//Construct the second diagonal and check it | |
if(j == 3) { | |
String diag2 = board[0].charAt(3) + "" + board[1].charAt(2) + "" + board[2].charAt(1) + "" + | |
board[3].charAt(0); | |
if(diag2.indexOf(".") < 0) { | |
String res4 = check(diag2); | |
if(res4 != null) { | |
return res4; | |
} | |
} else { | |
notComplete = true; | |
} | |
} | |
} | |
} | |
} | |
return notComplete ? "Game has not completed" : "Draw"; | |
} | |
/** | |
* | |
* @param str | |
* @return | |
*/ | |
private String check(String str) { | |
if(str.equals(xWin1) || str.equals(xWin2) || str.equals(xWin3) || | |
str.equals(xWin4) || str.equals(xWin5)) { | |
return "X won"; | |
} | |
if(str.equals(oWin1) || str.equals(oWin2) || str.equals(oWin3) || | |
str.equals(oWin4) || str.equals(oWin5)) { | |
return "O won"; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment