This is my assignment:
One of the most fundamental things we do in gaming is make decisions. These decisions are based on the current state of the game environment, user inputs, and other factors. Even something as simple as Tic-Tac-Toe requires decision making if the computer is playing against a human. In this lab we concentrate on analyzing the Tic-Tac-Toe board to determine if X or O has won the game.
Consider the following Tic-Tac-Toe board:

Clearly, X has won the game. But how would the computer know this?
Let us represent the board as a two-dimensional array of numbers, with three rows and three columns. A zero in any location means the board position is blank. A one in any position represents an X. A two in any position represents an O.
In this example, there are one’s in positions [0][0], [1][1], [2][1], and [2][2]. You should be able to figure out where the zero’s and two’s are.
Your program must do the following:
1. Read a two-dimensional array and display the game board as shown above.
2. Examine all three rows, all three columns, and both diagonals to see if there are three one’s (X’s) in a row. If so, display a message that says X has won the game.
3. Examine all three rows, all three columns, and both diagonals to see if there are three two’s (O’s) in a row. If so, display a message that says O has won the game.
4. If neither X nor O has won the game, and there are no blank board positions, then display a message that says no one has won the game.
5. If neither X nor O has won the game and there are blank board positions, then display a message that says the game continues.
Here are the board scenarios you must use to test your program. You must determine the arrays necessary to represent these boards. Put the six arrays in your program and pass them one at a time to a routine that will display the board and check the X’s and O’s to see if there is a winner.
Only one execution will be required, since your program will display and check all six board scenarios when it runs.

It is easy to pass an array to a method. Just declare an array variable in the method definition and access the array within the method as you would any other array.
this is the code that I have so far:
class Threeinarow
{
public static void main(String args[]) //Input for each board.
{
int array1[][] = {{1,2,0},{2,1,0},{2,1,1}};
int array2[][] = {{1,2,2},{1,2,0},{2,1,1}};
int array3[][] = {{1,2,0},{2,1,0},{2,1,2}};
int array4[][] = {{2,2,1},{1,1,2},{2,1,1}};
int array5[][] = {{1,2,0},{2,2,0},{1,1,1}};
int array6[][] = {{1,2,2},{2,1,2},{1,1,2}};
testarray(array1);
testarray(array2);
testarray(array3);
testarray(array4);
testarray(array5);
testarray(array6);
}
}
public void testarray (int array[][])
{
bool xwin = false;
bool owin = false;
bool nwin = false;
//checks to see if X won
for(int i = 0; i < 3; i++)
{
if(array[i][0] == 1 & array[i][1] == 1 & array[i][2] == 1)xwin = true;
if(array[0][i] == 1 & array[1][i] == 1 & array[2][i] == 1)xwin = true;
}
if(array[0][0] == 1 & array[1][1] == 1 & array[2][2] == 1)xwin = true;
if(array[2][0] == 1 & array[1][1] == 1 & array[0][2] == 1)xwin = true;
//checks to see if O won
for(int j = 0; j < 3; j++)
{
if(array[i][0] == 2 & array[i][1] == 2 & array[i][2] == 2)owin = true;
if(array[0][i] == 2 & array[1][i] == 2 & array[2][i] == 2)owin = true;
}
if(array[0][0] == 2 & array[1][1] == 2 & array[2][2] == 2)owin = true;
if(array[2][0] == 2 & array[1][1] == 2 & array[0][2] == 2)owin = true;
//checks to see if no one won
for(int k = 0; k < 3; k++)
{
if(array[i][0] == 0 || array[i][1] == 0 || array[i][2] == 0)nwin = true;
}
if(xwin == true)System.out.println("X has won the game.");
if(owin == true)System.out.println("O has won the game.");
if(nwin == true)System.out.println("No one has won the game.");
if((xwin == owin) == (nwin == false))System.out.println("The game continues.");
}
Can anyone guide me in finishing this? It would be really appreciated. I keep getting the error "class,interface, or enum expected"
Thanks
This post has been edited by jlm476: 25 March 2009 - 01:32 AM

New Topic/Question
Reply



MultiQuote



|