• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

After two days, I still can't see the obvious.

 
Jamie Cole
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've been trying for two days to debug a programming exercise I'm writing. The applet is a simple game where two players, white and black, take turn placing pieces on a board. Some details are not filled in yet, such as making sure the users only place pieces on empty squares, but everything that's done so far functions properly, except that I can't get the program to switch from placing white pieces to placing black ones.
The code that should switch from white to black is in bold print in the checkForWin method near the end. Here's that bit of code, pared down, then the whole applet. I'm hoping someone can spot the problem easily.
if (player == WHITE)
{
player = BLACK;
message.setText("Black's turn ");
}
else
{
player = WHITE;
message.setText("White's turn ");
}
******** APPLET ********
/* In this game, the white and black players take turns placing a piece on a 13X13 board.
When a player gets five pieces in a row, that player wins. */
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class GoMokuGame extends Applet
{
public void init()
{
setLayout(null);
setBackground(new Color(0, 180, 0));
MokuCanvas3 board = new MokuCanvas3();
add(board);
board.newGameButton.setBackground(Color.lightGray);
add(board.newGameButton);
board.resignButton.setBackground(Color.lightGray);
add(board.resignButton);
board.message.setForeground(Color.green);
board.message.setFont(new Font("Serif", Font.BOLD, 14));
add(board.message);
board.setBounds(16,16,172,172); // 13*12=156 +12 lines +4 border
board.newGameButton.setBounds(210, 60, 100, 30);
board.resignButton.setBounds(210, 120, 100, 30);
board.message.setBounds(0, 200, 330, 30);
}
}
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
class MokuCanvas3 extends Canvas implements MouseListener, ActionListener
{
Button newGameButton;
Button resignButton;
Label message;
boolean gameInProgress;
int row, col;
public static final int BLACK = 1, WHITE = 2, EMPTY = 0;
int player;
int[][] moveGrid = new int[13][13];
public MokuCanvas3()
{
setBackground(Color.lightGray);
addMouseListener(this);
setFont(new Font("Serif", Font.BOLD, 14));
newGameButton = new Button("New Game");
newGameButton.addActionListener(this);
resignButton = new Button("Resign");
resignButton.addActionListener(this);
message = new Label("", Label.CENTER);
doNewGame();
}
public void doNewGame()
{
if (gameInProgress == true)
{
message.setText("Finish this game first");
return;
}
else
{
message.setText("White's turn");
resignButton.setEnabled(true);
newGameButton.setEnabled(false);
gameInProgress = true;
for (int i = 0; i < 13; i++)
{
for (int j = 0; j < 13; j++)
{
moveGrid[i][j] = EMPTY;
}
}
player = WHITE;
repaint();
}
}
public void doResign()
{
if (gameInProgress == false)
{
message.setText("Must be in game to resign");
return;
}
else
{
if (player == WHITE)
message.setText("White resigns, Black wins");
if (player == BLACK)
message.setText("Black resigns, White wins");
resignButton.setEnabled(false);
newGameButton.setEnabled(true);
gameInProgress = false;
}
}
public void gameWon(int player)
{
if (player == WHITE)
message.setText("White wins");
if (player == BLACK)
message.setText("Black wins");
resignButton.setEnabled(false);
newGameButton.setEnabled(true);
gameInProgress = false;
}
public void actionPerformed(ActionEvent evt) {
// Respond to user's click on one of the two buttons.
Object src = evt.getSource();
if (src == newGameButton)
doNewGame();
else if (src == resignButton)
doResign();
}

public void mousePressed(MouseEvent evt)
{
col = (evt.getX() - 2) / 13;
row = (evt.getY() - 2) / 13;
if (col >= 0 && col < 13 && row >= 0 && row < 13)
{
recordMove(player, row, col);
}
}
public void recordMove(int player, int row, int col)
{
moveGrid[row][col] = player;
repaint();
checkForWin(player, row, col);
}
public void paint(Graphics g)
{
g.setColor(Color.black);
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
g.drawRect(1, 1, getSize().width - 3, getSize().height - 3);
for (int i = 1; i < 13; i++)
{
g.drawLine(1 + 13*i, 0, 1 + 13*i, getSize().height);
g.drawLine(0, 1 + 13*i, getSize().width, 1 + 13*i);
}
for (row = 0; row < 13; row++)
{
for (col = 0; col < 13; col++)
{
if (moveGrid[row][col] == WHITE)
{
g.setColor(Color.white);
g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);
}
else if (moveGrid[row][col] == BLACK)
{
g.setColor(Color.black);
g.fillOval(3 + 13*col, 3 + 13*row, 10, 10);
}
}
}
}
public void checkForWin(int player, int row, int col)
{
int startingRow = row;
int startingCol = col;
// check each direction both ways
if ( doCount(1, 0, startingRow, startingCol) >= 5)
{
gameWon(player);
}
else if ( doCount(0, 1, startingRow, startingCol) >= 5)
{
gameWon(player);
}
else if ( doCount(1, 1, startingRow, startingCol) >= 5)
{
gameWon(player);
}
else if ( doCount(1, -1, startingRow, startingCol) >= 5)
{
gameWon(player);
}
//
else
{
int recordCount = 0; // this variable inserted only for debugging
if (player == WHITE)
{
player = BLACK;
recordCount++;
message.setText("Black's turn " +recordCount);
}
else if (player == BLACK) // redundant, but added for clarity
{
player = WHITE;
recordCount++;
message.setText("White's turn " +recordCount);
}
}
//

}
public int doCount(int dirX, int dirY, int row, int col)
{
int count = 1;
int currentRow = row;
int currentCol = col;
int startingRow = row;
int startingCol = col;
currentRow = startingRow + dirX;
currentCol = startingCol + dirY;
while (currentRow >= 0 && currentRow < 13 && currentRow >= 0 && currentRow < 13 && moveGrid[currentRow][currentCol] == player)
{
count++;
currentRow += dirX;
currentCol += dirY;
}
currentRow = row - dirX;
currentCol = col - dirY;
while (currentRow >= 0 && currentRow < 13 && currentRow >= 0 && currentRow < 13 && moveGrid[currentRow][currentCol] == player)
{
count++;
currentRow -= dirX;
currentCol -= dirY;
}
return count;
}
public Dimension getPreferredSize()
{
return new Dimension(172, 172);
}
public Dimension getMinimumSize()
{
return new Dimension(172, 172);
}
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
}


[This message has been edited by Jamie Cole (edited November 19, 2000).]
[This message has been edited by Jamie Cole (edited November 19, 2000).]
[This message has been edited by Jamie Cole (edited November 19, 2000).]
 
Stephanie Grasson
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jamie,
I made a few modifications to your code and it seems to be running now. There were some ArrayOutOfBoundsExceptions generated in the doCount() method, probably because of the way the while loops were written.
I think, however, that the main problem was that player was being passed into methods as an argument. Because player is a member of class MokuCanvas3, it does not need to be passed in this way. It can be accessed directly. What was happening was that when you passed the variable called player into the method checkForWin(), the local copy of player was being changed, not the class copy. That was why the player never changed from white to black. I include the edit code below:

Hope this helps.
Let us know if you still have problems.
Stephanie
 
Jamie Cole
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Many thanks, Stephanie. I understand completely now. It probably would have bothered me for weeks without your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic