Home > Back-end >  JAVA implementation analysis of tic-tac-toe
JAVA implementation analysis of tic-tac-toe

Time:09-28

 import Java IO. ByteArrayOutputStream;
Import the Java. IO. PrintStream;
The import static org. Junit. Assert. *;
Public class Game
{
Game Game=null;
Result the Result=null;
PrintStream console=null;
The static ByteArrayOutputStream bytes=null;
String lineSeparator=System. GetProperty (" line. The separator ");

Public Result playGame (String s) {
String STR="";
The Result flag=Result. GAMING;//is used to represent the game state X, Y, no victory, and no end game, began, the default value is
String [] sArray=s.s plit (", ");//break up characters, form of A1, B1, B2, B3, C3 array
Int k=1;//intermediate variable
The int data []=new int [9].//by nine elements of an array of board state
Put//1 X position, to put the location of the 2 is O, zero is not accounted for position,

for(int j=0; jIf (j % 2==0) k=1; The else k=2;
//sArray [j]. Journal of charAt (0) - 'A' extraction and the first character in the string of "A1" 'A' is presupposed, made in the order of the columns
//sArray [j]. Journal of charAt (1) - '0') extraction and the second character in the string of "A1" '0' is presupposed, and get the same order
//the number of columns * 3 + get in position in the array lines - 1
Data [(sArray [j]. Journal of charAt (1) - '0' - 1) * 3 + (sArray [j]. Journal of charAt (0) - 'A')]=k;

STR +="A B C" + lineSeparator;
for(int i=0; i<9. I++) {
If (I==0) STR +="1";
If (I==3) STR +="2";
If (I==6) STR +="3";
If (data [I]==0) STR +="_";
If (data [I]==1) STR +="X";
If (data [I]==2) STR +="O";
If (I==2 | | I==5 | | I==8) STR +=lineSeparator;
}
}

For (k=0; K<9. K++) if (data [k]==0) break;
If (k==9) flag=Result. The DRAW.//if not 0 (no position in the array element,

//analysis of X - a=1
the condition of theInt a=1;
If (data [0]==a & amp; & Data [1]==a & amp; & Flag data [2]==a)=Result. X_WIN;
If (data [3]==a & amp; & Data [4]==a & amp; & Data [5] flag===a) Result. X_WIN;
If (data [6]==a & amp; & Data [7]==a & amp; & Data [8] flag===a) Result. X_WIN;
If (data [0]==a & amp; & Data [3]==a & amp; & Flag data [6]==a)=Result. X_WIN;
If (data [1]==a & amp; & Data [4]==a & amp; & Data [7] flag===a) Result. X_WIN;
If (data [2]==a & amp; & Data [5]==a & amp; & Data [8] flag===a) Result. X_WIN;
If (data [0]==a & amp; & Data [4]==a & amp; & Data [8] flag===a) Result. X_WIN;
If (data [2]==a & amp; & Data [4]==a & amp; & Flag data [6]==a)=Result. X_WIN;
If (flag!=Result. X_WIN)//if X be, whether the analysis O -
{
a=2;
If (data [0]==a & amp; & Data [1]==a & amp; & Flag data [2]==a)=Result. O_WIN;
If (data [3]==a & amp; & Data [4]==a & amp; & Data [5] flag===a) Result. O_WIN;
If (data [6]==a & amp; & Data [7]==a & amp; & Data [8] flag===a) Result. O_WIN;
If (data [0]==a & amp; & Data [3]==a & amp; & Flag data [6]==a)=Result. O_WIN;
If (data [1]==a & amp; & Data [4]==a & amp; & Data [7] flag===a) Result. O_WIN;
If (data [2]==a & amp; & Data [5]==a & amp; & Data [8] flag===a) Result. O_WIN;
If (data [0]==a & amp; & Data [4]==a & amp; & Data [8] flag===a) Result. O_WIN;
If (data [2]==a & amp; & Data [4]==a & amp; & Flag data [6]==a)=Result. O_WIN;
}

//the STR string into a ByteArrayOutputStream.
Bytes=new ByteArrayOutputStream ();
Bytes. Write (STR) getBytes (), 0, STR., getBytes (.) length);

return flag;//return one of the four state
}

Public static void main (String [] args)
{
Game Game=new Game ();
Result the Result=game. PlayGame (" A1, B1, B2, B3, C3 ");
The test (result);
Result=game. PlayGame (A1, A2, B1, B2, C1 "");
The test (result);
Result=game. The playGame (" C1, A2, C2, B2, C3 ");
The test (result);
Result=game. The playGame (" C1, A2, C2, C3, B2, A3, A1, B1, B3 ");
The test (result);
Result=game. The playGame (" A1, C1, A2, B2, B1, A3 ");
The test (result);
Result=game. PlayGame (" A1, A2, B1, B2, C3, and C2 ");
The test (result);
Result=game. PlayGame (" C1, A2 ");
The test (result);

Try {
Game. Test1 ();//not to catch exceptions, it is right,
} the catch (Exception e)
{
e.printStackTrace();
}


}
Public static void test Result (Result) {
String STR=bytes. The toString ();
System.out.println(str);
If (result==result. X_WIN) System. Out. The println (" X Player WINs the game. ");
If (result==result. O_WIN) System. Out. The println (" O Player WINs the game. ");
If (result==result. The DRAW) System. Out. The println (" No one WINs. ");
If (result==result. GAMING) System. Out. The println (" the Game is not over. ");
}
@ org. Junit. Test
Public void test1 () throws the Exception {
Game Game=new Game ();
Result=game. The playGame (" A1, B1, B2, B3, C3 ");
AssertEquals (Result) X_WIN, Result);
AssertEquals (
"A B C" + lineSeparator
+ "1 X _ _" + lineSeparator
+ 2 _ _ _ "+ lineSeparator
+ "3 _ _ _" + lineSeparator
"A B C" + + lineSeparator
"1 + X O _" + lineSeparator
+ 2 _ _ _ "+ lineSeparator
+ "3 _ _ _" + lineSeparator
"A B C" + + lineSeparator
"1 + X O _" + lineSeparator
+ 2 _ _ X "+ lineSeparator
+ "3 _ _ _" + lineSeparator
"A B C" + + lineSeparator
"1 + X O _" + lineSeparator
+ 2 _ _ X "+ lineSeparator
nullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related