| Author |
Message |
GD123 Grasshopper
Joined: 20 Dec 2004 Posts: 4
|
Posted: Mon Dec 20, 2004 1:18 am Post subject: Noob question on program errors |
|
|
Hi, I've been programming java for about 3 montsh and my class has recently had an assignment to complete a tictacto program. I've got it planned out mostly (just need to write a function for displaying a tie), but unfortanely my syntax is screwd up.
Specific problems: I need to find a way to generate random ints so that i can specify an array element, or find a way to specify array elements without using array[x][y]
-need to find the correct syntax for comparing strings within an array
copy and paste it into notepad to make it look much nicer, and thanks for any help
| Code: |
import java.io.*;
import java.lang.Math.*;
-find out how to check for a blank space
public class Newlab8
{
public static String[][] board = new String[3][3]; //the main board in the game
public static void main(String args[])throws IOException
{
BufferedReader keybd = new BufferedReader(new InputStreamReader(System.in));
for (int count = 0; count < 5; count++){
System.out.println("enter the column you would like to move to");
int a= Integer.parseInt(keybd.readLine());
System.out.println("now enter the row");
int b= Integer.parseInt(keybd.readLine());
if (board[a][b].equals(null)){ //tests to see if the space is blank
board[a][b] = "x";
}
boardprinter(); //prints the board
if (count == 4 && DidYouWin()) //tests if you win
{
System.out.println("You win");
count = 5;
}
if (count == 4 && DidComputerWin()) //tests if the computer has won
{
System.out.println("You lose!");
count = 5;
}
}
}
public static void boardprinter() //prints the board
{
for (int i = 0; i < board.length; i++){
for(int j = 0; j < 3; j++){
if (j == 2){
System.out.println(board[i][j]);
}
else{
System.out.print(" "+board[i][j] + " ");
}
}
}
}
public static boolean DidYouWin() //tests to see if you won
{
int c = 0;
int d = 0;
return board[c][d].equals(new String[]{"x"}) && board[c][d+1].equals(new String[]{"x"}) && board[c][d+2].equals(new String[]{"x"});
return board[c+1][d].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+1][d+2].equals(new String[]{"x"});
return board[c+2][d].equals(new String[]{"x"}) && board[c+2][d+1].equals(new String[]{"x"}) && board[c+2][d+2].equals(new String[]{"x"});
//returns true if they are in a horizontal line
return board[c][d].equals(new String[]{"x"}) && board[c+1][d].equals(new String[]{"x"}) && board[c+2][d].equals(new String[]{"x"});
return board[c][d+1].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+2][d+1].equals(new String[]{"x"});
return board[c][d+2].equals(new String[]{"x"}) && board[c+1][d+2].equals(new String[]{"x"}) && board[c+2][d+2].equals(new String[]{"x"});
//returns true if in a vertical line
return board[c][d].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+2][d+2].equals(new String[]{"x"});
return board[c][d+2].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+2][d].equals(new String[]{"x"});
//returns true if diagonal
}
public static boolean DidComputerWin() //tests to see if the computer won
{
int c = 0;
int d = 0;
return board[c][d].equals(new String[]{"o"}) && board[c][d+1].equals(new String[]{"o"}) && board[c][d+2].equals(new String[]{"o"});
return board[c+1][d].equals(new String[]{"o"}) && board[c+1][d+1].equals(new String[]{"o"}) && board[c+1][d+2].equals(new String[]{"o"});
return board[c+2][d].equals(new String[]{"o"}) && board[c+2][d+1].equals(new String[]{"o"}) && board[c+2][d+2].equals(new String[]{"o"});
//returns true if they are in a horizontal line
return board[c][d].equals(new String[]{"o"}) && board[c+1][d].equals(new String[]{"o"}) && board[c+2][d].equals(new String[]{"o"});
return board[c][d+1].equals(new String[]{"o"}) && board[c+1][d+1].equals(new String[]{"o"}) && board[c+2][d+1].equals(new String[]{"o"});
return board[c][d+2].equals(new String[]{"o"}) && board[c+1][d+2].equals(new String[]{"o"}) && board[c+2][d+2].equals(new String[]{"o"});
//returns true if in a vertical line
return board[c][d].equals(new String[]{"o"}) && board[c+1][d+1].equals(new String[]{"o"}) && board[c+2][d+2].equals(new String[]{"o"});
return board[c][d+2].equals(new String[]{"o"}) && board[c+1][d+1].equals(new String[]{"o"}) && board[c+2][d].equals(new String[]{"o"});
//returns true if diagonal
}
public static void ComputerMove() //way for the computer to move
{
int r = 0;
int c = 0;
do
{
r= Math.random(0,2);
c = Math.random(0,2);
}
while(board[r][c] != null)
board[r][c] = "o";
}
} //pvsm ends here
|
|
|
| Back to top |
|
 |
aschenta *narf*
Joined: 07 May 2003 Posts: 548 Location: Windsor Ontario Canada
|
Posted: Mon Dec 20, 2004 9:11 am Post subject: |
|
|
| Code: |
board[0][0].equals("x")
// for non case sensitive compare use
board[0][0].equalsIgnoreCase("x")
|
equals can compare a string to itself. "x" is considered a string so you can just pass that. |
|
| Back to top |
|
 |
Blankety Blank Man Ashigaru
Joined: 22 Oct 2004 Posts: 180
|
Posted: Mon Dec 20, 2004 11:29 am Post subject: |
|
|
| GD123 wrote: | | I need to find a way to generate random ints so that i can specify an array element, or find a way to specify array elements without using array[x][y] |
I don't quite know what you mean by that last part, but to generate random numbers, you need to import java.util.Random
the constructor is Random (variable) = new Random(seed); you can have the seed be a specific number (so that it will always make the same numbers in the same order each time you run the program), or not have a seed at all, and the computer will take the computer clock as the seed whenever it reaches the constructor (in milliseconds since jan. 19whatever, so the seed will never be the same)
then, you just do int (variable2) = (varibale).nextInt(number); where number is either nothing (to generate a number between 0 and 1... i think... i never use that, too confusing), or if it is a number, the range for the random number will be 0...number-1
| GD123 wrote: | | -need to find the correct syntax for comparing strings within an array |
what do you mean? something like this?
if(array[x][y].equals(array[a][b]))
also:
| Code: | return board[c][d].equals(new String[]{"x"}) && board[c][d+1].equals(new String[]{"x"}) && board[c][d+2].equals(new String[]{"x"});
return board[c+1][d].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+1][d+2].equals(new String[]{"x"});
return board[c+2][d].equals(new String[]{"x"}) && board[c+2][d+1].equals(new String[]{"x"}) && board[c+2][d+2].equals(new String[]{"x"});
//returns true if they are in a horizontal line
return board[c][d].equals(new String[]{"x"}) && board[c+1][d].equals(new String[]{"x"}) && board[c+2][d].equals(new String[]{"x"});
return board[c][d+1].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+2][d+1].equals(new String[]{"x"});
return board[c][d+2].equals(new String[]{"x"}) && board[c+1][d+2].equals(new String[]{"x"}) && board[c+2][d+2].equals(new String[]{"x"});
//returns true if in a vertical line
return board[c][d].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+2][d+2].equals(new String[]{"x"});
return board[c][d+2].equals(new String[]{"x"}) && board[c+1][d+1].equals(new String[]{"x"}) && board[c+2][d].equals(new String[]{"x"}); | ^all except that first return statement are unreachable. a return statement can only be the last command in the method.
secondly, you are comparing a string (board[c][d]) with a string array (new String[]{"x"})... that doesn't work
finally... why do you have to make a tic tac toe game after only 3 months of java? @_x
after 3 months, i was learning how to make separate classes and methods, not how to do this stuff! |
|
| Back to top |
|
 |
GD123 Grasshopper
Joined: 20 Dec 2004 Posts: 4
|
Posted: Mon Dec 20, 2004 6:36 pm Post subject: |
|
|
Mucho thanks. I just realized how dumb my mistakes were =P, like the return part, and the equals thing. Time to kick ass and chew bubblegum!
 |
|
| Back to top |
|
 |
Blankety Blank Man Ashigaru
Joined: 22 Oct 2004 Posts: 180
|
Posted: Tue Dec 21, 2004 1:26 am Post subject: |
|
|
| i thouhgt i ate all your bubblegum? |
|
| Back to top |
|
 |
Ankou Spam Mod
Joined: 22 Oct 2004 Posts: 1201 Location: Wisconsin
|
Posted: Tue Dec 21, 2004 1:37 am Post subject: |
|
|
If you ate all his bubblegum, then he's got a good reason to kick some @$$.
 |
|
| Back to top |
|
 |
GD123 Grasshopper
Joined: 20 Dec 2004 Posts: 4
|
Posted: Tue Dec 28, 2004 7:52 pm Post subject: |
|
|
| no way man, no need to kik ass, aced the course, and i can now move on to other l337 programs like blackjack foo |
|
| Back to top |
|
 |
|