Tic-Tac-Java
Hello again. I just finished writing this tic-tac-toe program for Java. Thought I'd share, because I had a pretty good time writing it. Below you'll find my code, obviously, with comments thrown in to elucidate just what's going on. Or, at least, what I think is supposed to be going on.
/*
* Created on Apr 23, 2005
* by J. Scott Jewell
* for CAS 174
* Description:
*
*/
public class ticInOut {
public static void main(String[] args) {
while(true){ Don't stop looping until I tell you.
ticTacToe game = new ticTacToe(); Clears the board, etc.
int breaker = 0;
System.out.println("Welcome to Tic-Tac-Toe. The board and its positions look like this:");
System.out.println(game.introBoard); Displays a map of board positions.
while(!game.won()){
for(int i = 0; i < 9; i++){ Make sure the game lasts nine moves or less.
do {
if (game.won())
break; Stop playing when the game is over.
System.out.println("" + game.getPlayer() + ": Where will you make your move?");
game.setChar(ConsoleIn.readLineInt());
System.out.println(game.toString());Prints out the current board
}
while (game.full);If the spot they picked is full, keep asking them where they'd like to go.
breaker = i;
}
if(breaker >= 8)
break; Breaks the loop after nine moves
}
if(game.won()){
System.out.println("Player " + game.getPlayer() + " is the winner!" );
}
else System.out.println("Shucks! Cat's Game!");
System.out.println("Press enter to play again.");
System.out.println("Enter anything else to exit.");
String loop = ConsoleIn.readLine();I don't think I'll ever understand this. My java class never taught us how to handle input and output, instead making us use a pre-made program to handle it. What gives?
if (!(loop.equals("")))
System.exit(0); Implements the "press enter to play again" bit.
else continue;
}
}
}
A sample game looks like:
Welcome to Tic-Tac-Toe. The board and its positions look like this:
1 | 2 | 3
-----------
4 | 5 | 6
-----------
7 | 8 | 9
X: Where will you make your move?
5
| |
-----------
| X |
-----------
| |
O: Where will you make your move?
1
O | |
-----------
| X |
-----------
| |
X: Where will you make your move?
3
O | | X
-----------
| X |
-----------
| |
O: Where will you make your move?
2
O | O | X
-----------
| X |
-----------
| |
X: Where will you make your move?
7
O | O | X
-----------
| X |
-----------
X | |
Player X is the winner!
Press enter to play again.
Enter anything else to exit.
q
Obviously, there's a whole other class containing all the background information for the program, such as win conditions (Every space got a score. -1 for X, 1 for 0. The "win" boolean was set to true if any valid combination of spaces added to either 3 or -3. Otherwise, after nine moves, it was a cat's game.), the stupid, stupid two-dimensional array they made us use for reasons I cannot fathom, a little routine to generate the play-by-play boards, etc. It's a bit long and technical to put in here as code, but it works out in the end.
When I was finally confident that everything was working correctly, I played a few games against Hochstetler. Watching him watch the screen, carefully ponder his moves, and place his X's without any hangups whatsoever made me really, really happy. I'm not sure why. Just proud of my work, I guess. How strange is that?
0 Comments:
Post a Comment
<< Home