// *** PART 0
// Put the names of the members of your group here
// Put your period here
// ***
// Tic Tac Toe Project
// For more instructions look at the project webpage at
http://home.earthlink.net/~casaburi/class/CS/projects.html
// these are needed for applets to work
import java.applet.Applet;
import java.awt.*;
// this is required for JOptionPane
import javax.swing.JOptionPane;
public class tictactoetemplate extends Applet
{
// defines a simple 2d array to store which player
has gone where
private int[][] board;
// defines the size of each square for the applet
private int sizesquare = 50;
// you can put code you want to run when the applet
first starts in this method
public void start()
{
// allow drawing...
setVisible(true);
// initialize the board...
board = new int[3][3];
// play the game
playgame();
}
// return true if the position on the board has been
claimed by a player
// (it would have a non-zero value stored there if
it is)
// Make sure to NOT allow invalid coordinates
(return true if x and y
// are invalid values)
private boolean istaken(int x, int y) {
// *** PART 2
// Replace the following
line with code that works as specified in the comments above
// ***
return true;
}
// return true if all of the given coordinates are
owned by the player
// (ie: if the values at those array positions are
the player's number)
// note: haswon(1,0,0,1,1,2,2) is asking if
player 1 has the positions (0,0) (1,1) and (2,2)
// in the format where the first integer in each
pair represents the X value, and the 2nd
// represents the Y value
private boolean haswon(int player, int x1, int y1,
int x2, int y2, int x3, int y3) {
// *** PART 3
// Replace the following
line with code that works as specified in the comments above
// ***
return false;
}
// return true if anyone has won the game yet, false
otherwise.
// You need to check each possible way to win at tic
tac toe to see if either player has
// won. (ie: check if they have a full row or
column, or the diagonals).
// Hint: there are a total of 8 ways a player can
win... Also, you are expected to
// use the above method (the other haswon) to help
you here.
private boolean haswon(int player) {
// *** PART 11
// Replace the following
line with code that works as specified in the comments above
// ***
return false;
}
// return true if the board is full (ie: every
single position is taken by either player1
// or player2
private boolean boardfull() {
// *** PART 4
// Replace the following
line with code that works as specified in the comments above
// ***
return false;
}
// This is where the action is...
private void playgame() {
// variables you can use for
the sake of reading in the coordinates from the players
int xpos=-1;
int ypos=-1;
// *** PART 5
// replace the condition of
the while loop (what now says xpos > 0) with one which basically
says that the while
// loop will run as long as
neither player has won the game, and as long as the board is not
totally full
// ***
while (xpos > 0) {
//
*** PART 6
//
Write code to keep on prompting player 1 for an x and y coordinate as
long as
//
the coordinates the player enters are invalid (less than 0 or greater
than 2)
//
and as long as the spot they are asking for is already taken
//
Hint: use another method to help figure out if something is
taken, and use JOptionPane
//
to read in the string from the user, and covert it over to an integer
//
Also, if you want the user to enter coordinates in the format where the
upper left
//
is 1,1 you need to subtract 1 off from each number the player enters
//
***
//
*** PART 7
//
At this point, we are out of that loop, and thus it should be safe to
assign the
//
board position the player asked for to player1 (assign it 1)
//
Write the code to do this, and then write more code to force Java to
redraw the screen
//
so we can see the update on the new spot that has been grabbed
//
***
//
*** PART 8
//
If player 1 did not just win and there is still space on the board, you
need to do
//
the exact same thing as you just did above for player 2. So
figure out of player1
//
just won, if player1 did not, copy the above 2 sections modifying them
so they
//
work for player 2
//
***
}
// *** PART 9
// At this point, someone
either won the game or the board is full. Find out if player 1
won the
// game, if so display some
sort of message saying as much, and then figure out if player 2 won
// and if so display some
sort of message saying as much, and otherwise, the board must be full
// and put up another
message in that case
// ***
}
// draw a single box (including if it's taken or not)
public void drawbox(int x, int y, Graphics2D g) {
g.setColor(Color.black);
// draw the box for the
square
g.drawRect(x*sizesquare,y*sizesquare,sizesquare,sizesquare);
// *** PART 10
// You need to fill in code
here to figure out if either player has this box
// and if so, to fill in the
box in a way to distinguish the box as being
// owned by one of the two
players. You can decide to use whatever
// design you wish to to
represent player 1 & which to represent player2
// ***
}
// this is called by Java every time the window
needs to be drawn or redrawn
public void paint(Graphics g)
{
Graphics2D page =
(Graphics2D) g;
// draw the board
page.setColor(Color.white);
page.fillRect(0, 0,
sizesquare*3, sizesquare*3);
// now draw each individual
box by calling drawbox for each possible position
for (int a = 0; a < 3;
a++) {
for
(int b = 0; b < 3; b++) {
drawbox(a,b,page);
}
}
// *** PART 1
// Change the following line
of code to include your group's names, and well, put any other messages
you want too
// ***
page.drawString("Tic Tac Toe
Game Template 1.0 - Mr. Casaburi's CSAP class, this is free software
(in case anyone cares)",0,sizesquare*3+20);
}
}