Requesting some help.
Author |
Message |
jMan
|
Posted: Fri Jan 25, 2008 7:26 pm Post subject: Requesting some help. |
|
|
Hello, I'm new to this forum as of posting, but I've been reading it for a little while now. I'm working on a little game and I'm stuck. Its similar to "Nim" or "Pearls before Swine". Anyways I having trouble accessing my columns in a array. I'm try to verify a move for my game. This is how the game works:
Its a single player game played against a computer. The point of the game is to make your opponent remove the last "bean". The board is made with a 2 dimensional array. This is what the game board looks like:
0
000
00000
0000000
000000000
Anyways if you know the rules of "Pearls Before Swine" theres no need to read this next part.
The human player moves first. The player can move as many beans as they want as long
as they all come from the same row. To move, the player must specify the row and
column of the bean to be removed. All beans to the right of the specified bean are also
removed. For instance, if you wanted to remove four beans from the third row, you
would enter 3 and 2 (you will have to take into consideration that arrays are indexed
starting at zero and adjust so that the player can count from 1). The result would be:
0
000
0
0000000
000000000
The computer then takes a turn removing a selected number of beans the result of which
is also displayed to the user. The computer should choose his moves intelligently (ie. it
should be difficult to beat). The player who must remove the last bean loses. Input
should be validated so that the player cannot crash the game.
Anyways I'm having trouble verifying the removal of a "bean". This is my code so far:
import java.util.*;
import java.util.Random;
public class beanGame
{
private int playingBoard[][] = { {0}, {0,0,0}, {0,0,0,0,0}, {0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0,0} };
private int beans, row, comRow, comBeans;
private boolean isRow = false, isBean = false;
private int counters[];
public static void main(String[] args)
{
beanGame game = new beanGame();
System.out.println("Welcome to the bean game.");
game.printBoard();
game.countBean();
game.userMove();
game.compMove();
}
public void printBoard()
{
for ( int i = 0; i < playingBoard.length; i++ )
{
for ( int j = 0; j < playingBoard[i].length; j++ )
{
System.out.print( playingBoard[ i ][ j ] + " " );
}
System.out.println( "" );
}
}
public void countBean()
{
counters = new int[ 5 ];
for ( int i = 0; i < playingBoard.length; i++ )
{
for ( int j = 0; j < playingBoard[i].length ; j++ )
{
if ( playingBoard[i][j] == 0 )
counters[i]++;
}
}
}
public void userMove()
{
Scanner scanner = new Scanner( System.in );
boolean isRow = false;
while( !isRow )
{
System.out.println( "What row would you like to remove beans from?" );
row = scanner.nextInt();
if ( row >= 1 && row <= 5 && counters[ row - 1 ] != 0 )
isRow = true;
else
System.out.println( "Not a valid move." );
}
while( !isBean )
{
System.out.println( "How many beans would you like to remove?" );
beans = scanner.nextInt();
if ( beans >= 1 && beans <= 9 && counters[ beans - 1 ] != 0)
isBean = true;
else
System.out.println( "Not a valid move." );
}
}
public void compMove()
{
Random r = new Random();
while( !isRow )
{
comRow = r.nextInt( 6 );
if (comRow >= 1 && comRow <= 5 && counters[ comRow - 1 ] != 0 )
isRow = true;
}
System.out.println( comRow );
while( !isBean )
{
comBeans = r.nextInt( 9 );
if (comBeans >= 1 && comBeans <= 9)
isBean = true;
}
System.out.println( comBeans );
}
}
Any help would be greatly appreciated. Thanks. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Tubs
![](http://members.lycos.co.uk/lovedtwice2000/phpBB2/images/avatars/187282473e3fd65a3a33b.gif)
|
Posted: Fri Jan 25, 2008 8:18 pm Post subject: Re: Requesting some help. |
|
|
Seems to me that it would be simpler to use a one-dimensional integer array to hold the number of 'beans' rather than a two dimensional one. |
|
|
|
|
![](images/spacer.gif) |
jMan
|
Posted: Fri Jan 25, 2008 8:41 pm Post subject: Re: Requesting some help. |
|
|
Haha, Ya I know it would be easier, but I'm going for a graphical aspect as well. If I can't get any further I'll take your advice probably and just do a single array. |
|
|
|
|
![](images/spacer.gif) |
|
|