



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Material Type: Lab; Class: COMPUTER SCIENCE I-LAB; Subject: COMPUTER SCIENCE; University: SUNY-Potsdam; Term: Unknown 1989;
Typology: Lab Reports
1 / 6
This page cannot be seen from the preview
Don't miss anything!
In this lab you will work with classes to implement a Tic-Tac-Toe game in FANG. This game will use components with “state” – that is, components that contain information about themselves in relation to the game.
Consider how you might make a simple version of Tic-Tac-Toe: your Game creates and positions nine sprites on the screen. Each sprite corresponds to one of the nine squares in the game. Then, in advance, you would check for mouse clicks, determining if any of the sprites was clicked on. If one is clicked and it is empty, you would update the sprite with the current player’s symbol and change whose turn it is. Along the way, you would check for winning and draws (Cat’s game).
Think about how your advance would look. Assuming the sprites are in an ArrayList, there would be a big loop with many nested if statements in it to check for a winner or a draw. But this code would be too complex to hold in your mind, all at once.
Programmers respond to this sort of complexity by abstration : that is, hiding the details of some rule or some component by writing methods with descriptive names that express a solution to the problem, and then by filling in the details of these methods at another time. Abstraction can also be achieved by breaking responsibility for the solution to the problem across multiple components.
The “simple” approach given above is difficult because the Tic-Tac-Toe game would be responsible for handling both game-level details and square-level details. A separation of responsibility is possible if we build “smart” squares that can handle the square-level details and then let the game handle the game-level details. This is a standard technique in object-oriented design, the creation of objects that “know what to do”.
Write down what activities a Tic-Tac-Toe game must support. Divide up your list into game- level and square-level activities. Ask yourselves the following questions:
Show us your work when you are done.
We will assume that each square is responsible for:
The game is then responsible for
This lab builds two cooperating classes, TicTacToe and TicTacToeSquare, each fulfilling one of these sets of requirements.
Note how the two classes cooperate: A TicTacToe object will need references to all of the squares so that it can call advance for each of them; A TicTacToeSquare object needs a reference to the game of which it is a part to be able to check for mouse clicks and to determine whose turn it is, when it needs to be updated. This means that, like the OneDie class, squares will be constructed with a reference to the game object.
Create a suitable directory (Lab11) in which you will do your work.
Start editing the TicTacToe and TicTacToeSquare classes. The first class should extend Game, and the second should extend CompositeSprite. You will be building this game one piece at a time. First, you will get the squares to draw in the right places. Then you will make the squares aware of mouse clicks. And finally you will make the game check for end-of-game conditions.
player 0 (“X”) is the first to play.
A TicTacToeSquare object needs a field content to track its own contents – that is, a blank, “X”, or “O”. You should use -1, 0, and 1 to correspond to blank, “X”, and “O”, respectively. Initially, make the content equal to -1 for empty.
Implement isEmpty (a public, Boolean method) in your TicTacToeSquare class and a getter method for the content.
To display the content, add a StringSprite field to your TicTacToeSquare class. The scale should be 1.0, and it should initially contain the empty string. Set its color to some light color and make sure the sprite is added to the composite sprite. When you set the content of your square, you will set the text to either X or O.
For testing purposes, set the string to something like # for testing, and verify that your squares will all have big #s displaying on the screen when you run your game.
Show us your work at this point. Be sure to set the string to an empty string before you continue to the next step.
Create an advance method in TicTacToeSquare that will be called from TicTacToe’s advance method. In the advance method for a TicTacToeSquare, do the following:
if ((this square is empty) && (the mouse has been clicked) && (this sprite intersects the mouse click)) { update content with the value of the current player update appearance of StringSprite call theGame.finishTurn() }
Now, in the TicTacToe advance method, call the advance method on every square on every frame. Also, write a stub for the method finishTurn. (This method should be public void with no parameters).
At this point, you should have running code that starts out with all blank squares and that turns each square to an X when you click on it.
Next you should implement finishTurn in the TicTacToe class: it takes no parameters and just changes whose turn it is. If turn was 0, make it 1 and if it was 1, make it 0.
At this point your program should change empty squares alternately to X s and O s until all the squares are filled. Show us your work.
Modify finishTurn to something like the following:
if (winner()) { // handle win: display a winning message and wait } else if (catsGame()) { // handle cat’s game: display an appropriate cat’s game message and wait } else { // change whose turn it is as before }
Next you need to write the two methods winner and catsGame. First, write stubs for them: each will have public visibility and will return a boolean. In your stubs, you should return false.
With these changes, your program should still behave exactly as before. Show us your work.
To implement your (currently stubbed out) catsGame method, you should look at all the squares and return false if any of them is empty. Otherwise, return true. Use the isEmpty method in the TicTacToeSquare class to check for being empty.