Tic-tac-toe using a two-dimensional array
No. 153
Q: |
This exercise exclusively deals with the visualization and bookkeeping of two humans playing Tic-tac-toe against each other. For the sake of clarification: It is not about humans playing a computer. So you do not (yet) have to implement an algorithm finding a player's best possible moves. The following video shows the intended behaviour: Figure 455. Two Tic-tac-toe players fighting each other.
Use a two-dimensional array to represent a game's state:
Depending on your choice you may also use a class
Discovering a “win” situation will be an essential part of this exercise. The following situations define a “win” state:
|
|||||||||||||||
A: |
Consider the following Maven project:
This implementation already uses a so called
A Java™
It is possible to wrap this solution into an executable Jar archive by adding:
This allows for console execution rather than using your IDE: goik@mi-ESPRIMO-P910 V1> mvn install
[INFO] Scanning for projects...
...
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running de.hdm_stuttgart.mi.sd1.connectfour.AppTest
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 sec
Results :
Tests run: 2, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- maven-jar-plugin:2.6:jar (default-jar) @ tictactoe ---
[INFO] Building jar: /.../P/Sd1/TicTacToe/V1/target/tictactoe-0.9.jar
[INFO]
[INFO] --- maven-install-plugin:2.5.2:install (default-install) @ tictactoe ---
[INFO] Installing /.../de/hdm-stuttgart/mi/sd1/tictactoe/0.9/tictactoe-0.9.jar
[INFO] Installing /ma/.../de/hdm-stuttgart/mi/sd1/tictactoe/0.9/tictactoe-0.9.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.242s
[INFO] Finished at: Wed Sep 23 12:54:34 CEST 2015
[INFO] Final Memory: 18M/196M
[INFO] ------------------------------------------------------------------------
As you can see this creates
goik@mi-ESPRIMO-P910 ~> java -jar /ma/goik/.m2/.../tictactoe-0.9.jar Numbering scheme: 0|1|2 -+-+- 3|4|5 -+-+- 6|7|8 Jim, please enter next field's number: |