Created
May 1, 2019 02:08
-
-
Save deathmarine/59793a560c0146cfd4be9d2908bd2a76 to your computer and use it in GitHub Desktop.
Tie example
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
int count = 0; | |
public boolean changeToken(int index) { | |
// make sure this space is blank, if not return | |
if (gameArray[index] != Token.Blank) { | |
System.out.println("Space is already used"); | |
return false; | |
} | |
// get token for player | |
Token t; | |
if (p1Turn) { | |
t = Token.X; | |
} else { | |
t = Token.O; | |
} | |
// set token at index | |
gameArray[index] = t; | |
System.out.println("changes token"); | |
// see if won, if so, game is over and return true | |
count++; | |
if (checkForWin(index)) { | |
System.out.println("Wins!"); | |
gameOver = true; | |
return true; | |
} else if(checkForTie()) { | |
count = 0; | |
gameOver = true; | |
return true; | |
} | |
// change turns | |
p1Turn = !p1Turn; | |
return true; | |
} | |
public boolean checkForTie() { | |
return count == 8; | |
} | |
public boolean isSpaceBlank(int index) { | |
if (gameArray[index] == Token.Blank) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
public boolean isP1Turn() { | |
return p1Turn; | |
} | |
public boolean checkForWin(int index) { | |
//TO DO: check to see if there is a winner. If so, set win field and return true; otherwise, return false | |
if(gameArray[0] == gameArray[1] && gameArray[1] == gameArray[2]) | |
win = Win.TOP_ROW; // Could probably do this with a case switch | |
if(gameArray[0] == gameArray[3] && gameArray[3] == gameArray[6]) | |
win = Win.LEFT_COL; | |
//Blah... If statement | |
//More If statements | |
//MOAR if statements | |
if(win != Win.NONE) | |
count = 0; | |
return win != Win.NONE; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment