Skip to content

Instantly share code, notes, and snippets.

@tatewake tatewake/TicTacToe.java
Last active Mar 14, 2017

Embed
What would you like to do?
public class TicTacToe
{
protected enum StateType
{
kUninitialized,
kDestroyed,
kEnd,
kOsTurn,
kXsTurn,
};
protected StateType mState = StateType.kUninitialized;
protected boolean EnterAskPlayAgain()
{
}
protected void EnterClearBoard()
{
}
protected void EnterDestroyed()
{
}
protected boolean EnterDidAnyoneWin()
{
}
protected boolean EnterDidXWin()
{
}
protected void EnterEnd()
{
}
protected boolean EnterIsBoardFull()
{
}
protected void EnterOsTurn()
{
}
protected void EnterPrintDraw()
{
}
protected void EnterPrintXWon()
{
}
protected void EnterPrintYWon()
{
}
protected void EnterStart()
{
}
protected boolean EnterWillXStart()
{
}
protected boolean EnterXPlaysNext()
{
}
protected void EnterXsTurn()
{
}
protected void OnConstruct()
{
assert(mState == StateType.kUninitialized);
if (mState == StateType.kUninitialized)
{
EnterStart();
EnterClearBoard();
if (EnterWillXStart())
{
mState = StateType.kXsTurn;
EnterXsTurn();
}
else
{
mState = StateType.kOsTurn;
EnterOsTurn();
}
}
}
protected void OnDestruct()
{
assert(mState == StateType.kEnd || mState == StateType.kOsTurn || mState == StateType.kXsTurn);
switch (mState)
{
case kEnd:
case kOsTurn:
case kXsTurn:
mState = StateType.kDestroyed;
EnterDestroyed();
break;
default:
break;
}
}
protected void OnPickSquare()
{
assert(mState == StateType.kOsTurn || mState == StateType.kXsTurn);
switch (mState)
{
case kOsTurn:
if (EnterDidAnyoneWin())
{
if (EnterDidXWin())
{
EnterPrintXWon();
if (EnterAskPlayAgain())
{
EnterClearBoard();
if (EnterWillXStart())
{
mState = StateType.kXsTurn;
EnterXsTurn();
}
else
{
EnterOsTurn();
}
}
else
{
mState = StateType.kEnd;
EnterEnd();
}
}
else
{
EnterPrintYWon();
if (EnterAskPlayAgain())
{
EnterClearBoard();
if (EnterWillXStart())
{
mState = StateType.kXsTurn;
EnterXsTurn();
}
else
{
EnterOsTurn();
}
}
else
{
mState = StateType.kEnd;
EnterEnd();
}
}
}
else
{
if (EnterIsBoardFull())
{
EnterPrintDraw();
if (EnterAskPlayAgain())
{
EnterClearBoard();
if (EnterWillXStart())
{
mState = StateType.kXsTurn;
EnterXsTurn();
}
else
{
EnterOsTurn();
}
}
else
{
mState = StateType.kEnd;
EnterEnd();
}
}
else
{
if (EnterXPlaysNext())
{
mState = StateType.kXsTurn;
EnterXsTurn();
}
else
{
EnterOsTurn();
}
}
}
break;
case kXsTurn:
if (EnterDidAnyoneWin())
{
if (EnterDidXWin())
{
EnterPrintXWon();
if (EnterAskPlayAgain())
{
EnterClearBoard();
if (EnterWillXStart())
{
EnterXsTurn();
}
else
{
mState = StateType.kOsTurn;
EnterOsTurn();
}
}
else
{
mState = StateType.kEnd;
EnterEnd();
}
}
else
{
EnterPrintYWon();
if (EnterAskPlayAgain())
{
EnterClearBoard();
if (EnterWillXStart())
{
EnterXsTurn();
}
else
{
mState = StateType.kOsTurn;
EnterOsTurn();
}
}
else
{
mState = StateType.kEnd;
EnterEnd();
}
}
}
else
{
if (EnterIsBoardFull())
{
EnterPrintDraw();
if (EnterAskPlayAgain())
{
EnterClearBoard();
if (EnterWillXStart())
{
EnterXsTurn();
}
else
{
mState = StateType.kOsTurn;
EnterOsTurn();
}
}
else
{
mState = StateType.kEnd;
EnterEnd();
}
}
else
{
if (EnterXPlaysNext())
{
EnterXsTurn();
}
else
{
mState = StateType.kOsTurn;
EnterOsTurn();
}
}
}
break;
default:
break;
}
}
public TicTacToe()
{
mState = StateType.kUninitialized;
OnConstruct();
}
public void finalize() throws Throwable
{
OnDestruct();
super.finalize();
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.