Reputation: 9
For some reason, my Move() function in the class TicTacToeBoard is not working.
Here is the main:
import java.util.*;
public class TicTakToe {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("What player are you(X or O)?");
String player = in.next();
System.out.println("What row do you want to play in(top, middle, bottom)?");
String row = in.next();
System.out.println("What column do you want to play in(left, center, right)?");
String column = in.next();
System.out.println(player + ", " + row + ", " + column);
TicTacToeBoard one = new TicTacToeBoard(player, row, column);
one.Move();
one.printBoard();
int counter = 1;
while(counter <= 9){
System.out.println("What player are you(X or O)?");
player = in.next();
System.out.println("What row do you want to play in(top, middle, bottom)?");
row = in.next();
System.out.println("What column do you want to play in(left, center, right)?");
column = in.next();
System.out.println(player + ", " + row + ", " + column);
one.Move();
counter ++;
one.printBoard();
}
}
}
Here is the class:
class TicTacToeBoard {
private int[][]board = {
{0,0,0},
{0,0,0},
{0,0,0} };
String row, column, player;
//Constructor
public TicTacToeBoard(String r, String c, String p){
row = r;
column = c;
player = p;
}
public void Move(){
if (row == "top" && column == "left" && player == "X"){
board[0][0] = 1;
}
if (row == "top" && column == "center" && player == "X"){
board[0][1] = 1;
}
if (row == "top" && column == "right" && player == "X"){
board[0][2] = 1;
}
if (row == "middle" && column == "left" && player == "X"){
board[1][0] = 1;
}
if (row == "middle" && column == "center" && player == "X"){
board[1][1] = 1;
}
if (row == "middle" && column == "right" && player == "X"){
board[1][2] = 1;
}
if (row == "bottom" && column == "left" && player == "X"){
board[2][0] = 1;
}
if (row == "bottom" && column == "center" && player == "X"){
board[2][1] = 1;
}
if (row == "bottom" && column == "right" && player == "X"){
board[2][2] = 1;
}
if (row == "top" && column == "left" && player == "Y"){
board[0][0] = 1;
}
if (row == "top" && column == "center" && player == "Y"){
board[0][1] = 1;
}
if (row == "top" && column == "right" && player == "Y"){
board[0][2] = 1;
}
if (row == "middle" && column == "left" && player == "Y"){
board[1][0] = 1;
}
if (row == "middle" && column == "center" && player == "Y"){
board[1][1] = 1;
}
if (row == "middle" && column == "right" && player == "Y"){
board[1][2] = 1;
}
if (row == "bottom" && column == "left" && player == "Y"){
board[2][0] = 1;
}
if (row == "bottom" && column == "center" && player == "Y"){
board[2][1] = 1;
}
if (row == "bottom" && column == "right" && player == "Y"){
board[2][2] = 1;
}
}
public void printBoard(){
for(int i = 0;i <= 2;i++){
for(int j = 0; j <= 2;j++){
System.out.print(board[i][j]);
}
System.out.println();
}
}
}
The output is:
What player are you(X or O)?
X
What row do you want to play in(top, middle, bottom)?
top
What column do you want to play in(left, center, right)?
left
X, top, left
000 000 000
What player are you(X or O)?
For some reason, the board ends up ass all zeros no matter what I input. Any ideas about what I'm doing wrong?
Upvotes: 0
Views: 90
Reputation: 4843
Replace your lines like
if (row == "top" && column == "left" && player == "X"){
board[0][0] = 1;
}
with
if ("top".equalsIgnoreCase(row) && "left".equalsIgnoreCase(column) && "X".equalsIgnoreCase(player)){
board[0][0] = 1;
}
The == operator asks if the two items are the same object ..not whether they have the same value.
Upvotes: 0
Reputation: 3376
The first problem in your code is in constructor of TicTacToeBoard
TicTacToeBoard(String r, String c, String p) the contstructor parameter expecting row,column and player but you are passing
TicTacToeBoard one = new TicTacToeBoard(player, row, column);
player, row and column Which is wrong.
Second thing is that you are not updating player,row,column value of TicTacToeBoard class in your while loop.Every time when you are taking input from user in while loop after that you have to set those values in TicTacToeBoard class also.
Upvotes: 2
Reputation: 32145
First of all, they are called methods in Java and not functions.
And change row == "...."
to row.equals(...)
because that's how we compare strings in java.
That should do it.
Upvotes: 0
Reputation: 2578
I think first you need to replace row == "top"
and other string comparision using String.equals(String)
methods
Upvotes: 2