Im making a text based battleship game and the player plays against the computer. 3 random 3 unit long ships are placed on the board, and I want the computer to be able to guess around where his last guess was if his last guess was a hit. (but I want it to work so that he keeps guessing around the same spot until he got a hit and keep guessing around there until he gets the whole ship, or 3 hits)
It works a bit; the computer will guess near his last guess if it was a hit, but if he misses that guess then he starts guessing randomly again. Can someone help me out a bit?
-getGuess() method is the one with the AI-
/*
* computer class to handle computers guesses/ etc
* most methods are copied from player class, but slightly altered to account for variable names
* Methods that havent been copied have comments
*/
public class Computer{
static int firstCo, secondCo;
static int[] guessedHits={7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
//int array to store last guess
static int[] lastGuess = new int[2];
//int array to store current guess
static int[] guess=new int[2];
public static int[] computerShip1=new int[6];
public static int[] computerShip2=new int[6];
public static int[] computerShip3=new int[6];
/*
* method to choose random guess for computer - but make it guess around last guess if last guess was a hit
* return guess coordinate numbers in an array
*/
public static int[] getGuess(){
int[] guess=new int[2];
int firstCo, secCo;
int ran; //random int between 0 and 1 - will help to make random choices for guesses
if(isHit(lastGuess[0],lastGuess[1])){
ran=(int)(Math.random()*2);
//if ran is 0 and last guesses x coordinate was correct, set next guess to last x, and next y to last y +1
if((ran==0 && lastGuess[0]==Player.playerShip1[0]) || (ran==0 && lastGuess[0]==Player.playerShip1[2]) || (ran==0 && lastGuess[0]==Player.playerShip1[4])){
guess[0]=lastGuess[0];
guess[1]=lastGuess[1]+1;
//if ran is 1 and last guesses x coordinate was correct, set next guess to last x, and next y to last y -1
}else if((ran==1 && lastGuess[0]==Player.playerShip1[0]) || (ran==1 && lastGuess[0]==Player.playerShip1[2]) || (ran==1 && lastGuess[0]==Player.playerShip1[4])){
guess[0]=lastGuess[0];
guess[1]=lastGuess[1]-1;
//if ran is 0 and last guesses y coordinate was correct, set next guess to last y, and next x to last x +1
}else if((ran==0 && lastGuess[1]==Player.playerShip1[1]) || (ran==0 && lastGuess[1]==Player.playerShip1[3]) || (ran==0 && lastGuess[1]==Player.playerShip1[5])){
guess[0]=lastGuess[0]+1;
guess[1]=lastGuess[1];
//if ran is 1 and last guesses y coordinate was correct, set next guess to last y, and next x to last x -1
}else if((ran==1 && lastGuess[1]==Player.playerShip1[1]) || (ran==1 && lastGuess[1]==Player.playerShip1[3]) || (ran==1 && lastGuess[1]==Player.playerShip1[5])){
guess[0]=lastGuess[0]-1;
guess[1]=lastGuess[1];
}
return guess;
}else{
guess[0]=(int)(Math.random()*7);
guess[1]=(int)(Math.random()*7);
return guess;
}
}
public static boolean isHit(int firstC, int secC){
for(int i=0; i<Player.playerShip1.length; i=i+2){
if(firstC==Player.playerShip1[i] && secC==Player.playerShip1[i+1]){
return true;
}
if(i==4){
break;
}
}
for(int i=0; i<Player.playerShip2.length; i=i+2){
if(firstC==Player.playerShip2[i] && secC==Player.playerShip2[i+1]){
return true;
}
if(i==4){
break;
}
}
for(int i=0; i<Player.playerShip3.length; i=i+2){
if(firstC==Player.playerShip3[i] && secC==Player.playerShip3[i+1]){
return true;
}
if(i==4){
break;
}
}
return false;
}
public static void addHits(int firstC, int secC){
int index=-1;
for(int i=0; i<guessedHits.length; i++){
if(guessedHits[i]==7){
index=i;
break;
}
}
guessedHits[index]=firstC;
guessedHits[index+1]=secC;
}
public static void setComputerShips(){
int randX, randY;
int direction; //will be random int 0-1, determines direction ship will extend(up/down, left/right)
randX=(int)(Math.random()*7);
randY=(int)(Math.random()*7);
direction=(int)(Math.random()*2);
computerShip1[0]=randX;
computerShip1[1]=randY;
if(direction==0){//extend upwards or downwards 2 units(y values change, x stays the same)
computerShip1[2]=randX;
computerShip1[4]=randX;
if(randY>3){//if y value is greater than 3, has to extend down or it wont fit
computerShip1[3]=randY-1;
computerShip1[5]=randY-2;
}else if(randY<2){//if y value is less than 2, has to extend up or it wont fit
computerShip1[3]=randY+1;
computerShip1[5]=randY+2;
}else{//if direction doesnt matter, just extend upwards
computerShip1[3]=randY+1;
computerShip1[5]=randY+2;
}
}else if(direction==1){//extends left or right 2 units(y values stay the same, x changes)
computerShip1[3]=randY;
computerShip1[5]=randY;
if(randX>3){//if x is greater than 3, must extend left or it wont fit
computerShip1[2]=randX-1;
computerShip1[4]=randX-2;
}else if(randX<2){//if x is less than 2, must extend right or it wont fit
computerShip1[2]=randX+1;
computerShip1[4]=randX+2;
}else{//if direction doesnt matter, just extend right
computerShip1[2]=randX+1;
computerShip1[4]=randX+2;
}
}
//do same for both other ships
do{
randX=(int)(Math.random()*7);
randY=(int)(Math.random()*7);
}while((randX==computerShip1[0] && randY==computerShip1[1])||(randX==computerShip1[2]&&randY==computerShip1[3])||(randX==computerShip1[4]&&randY==computerShip1[5]));
direction=(int)(Math.random()*2);
computerShip2[0]=randX;
computerShip2[1]=randY;
if(direction==0){
computerShip2[2]=randX;
computerShip2[4]=randX;
if(randY>3){
computerShip2[3]=randY-1;
computerShip2[5]=randY-2;
}else if(randY<2){
computerShip2[3]=randY+1;
computerShip2[5]=randY+2;
}else{
computerShip2[3]=randY+1;
computerShip2[5]=randY+2;
}
}else if(direction==1){
computerShip2[3]=randY;
computerShip2[5]=randY;
if(randX>3){
computerShip2[2]=randX-1;
computerShip2[4]=randX-2;
}else if(randX<2){
computerShip2[2]=randX+1;
computerShip2[4]=randX+2;
}else{
computerShip2[2]=randX+1;
computerShip2[4]=randX+2;
}
}
do{
randX=(int)(Math.random()*7);
randY=(int)(Math.random()*7);
}while((randX==computerShip1[0] && randY==computerShip1[1])||(randX==computerShip1[2]&&randY==computerShip1[3])||(randX==computerShip1[4]&&randY==computerShip1[5])||(randX==computerShip2[0] && randY==computerShip2[1])||(randX==computerShip2[2]&&randY==computerShip2[3])||(randX==computerShip2[4]&&randY==computerShip2[5]));
direction=(int)(Math.random()*2);
computerShip3[0]=randX;
computerShip3[1]=randY;
if(direction==0){
computerShip3[2]=randX;
computerShip3[4]=randX;
if(randY>3){
computerShip3[3]=randY-1;
computerShip3[5]=randY-2;
}else if(randY<2){
computerShip3[3]=randY+1;
computerShip3[5]=randY+2;
}else{
computerShip3[3]=randY+1;
computerShip3[5]=randY+2;
}
}else if(direction==1){
computerShip3[3]=randY;
computerShip3[5]=randY;
if(randX>3){
computerShip3[2]=randX-1;
computerShip3[4]=randX-2;
}else if(randX<2){
computerShip3[2]=randX+1;
computerShip3[4]=randX+2;
}else{
computerShip3[2]=randX+1;
computerShip3[4]=randX+2;
}
}
}
public static boolean hasWon(){
if(guessedHits[17]!=7)
return true;
else
return false;
}
}
Your getGuess() function is the one you're after right?
1) You never account for times when you guess the same spot twice. Make a boolean value that determines whether the coordinates you're attempting to guess haven't already been guessed.
2) Your method of keeping ship coordinates is very awkward where 0,2,4 are X coords while 1,3,5 are Y coords? You're better off creating a Ship class that handles coordinates, and checks like isHit.
public class Ship {
int[] xCoords = new int[3];
int[] yCoords = new int[3];
public boolean isHit(int x, int y) {
return (Arrays.asList(xCoords).contains(x) && Arrays.asList(yCoords).contains(y));
}
}
Then you can:
if (Player.ship1.isHit(guess[0],guess[1])) {
....
}
At the very heart of it you have a little ways to go. You'll get better responses here if you start working at the problem then come back with specific problems you may have. Try to be as concise as possible when giving code snippets because not many people will spend much time going through an entire class to find a line or two giving issues.
Good luck!
---PS---
I wrote a battleship game about 3-4 years ago with some fairly advanced AI. I'll link it here:
https://github.com/GrahamBlanshard/AI-Battleship/blob/master/prograham/battleship/player/AIPlayer.java
First, I apologize for the... lame code (I was a much younger programmer, I swear!). If you want to view it to get hints that is fine. A brief explanation:
At the heart of it you need to create some form of datatype that stores his hits. Once a "hit" is scored you push it to the datatype, I used a Stack. The shots that are successful hits get stored on the stack until the ship is sunk. At that point it removes shots from the stack that belonged to the ship that just sunk. If there are shots still on the stack it knows it has hit a second ship during that process and continues to guess in the area.
To accomplish this, it goes through phases:
1) Shoot randomly until a hit.
2) Shoot around that shot (use a random(4) call to get N/S/E/W direction)
-- Keep doing this until you score a second shot
3) Create a "line" with the two points and fire along it until the ship sinks or...
4) Reverse the line and shoot the other direction.
Does that give you a good start to work with?
That's a lot of code to look at. So for now I will give some general suggestions that come to mind:
When the computer AI gets a "hit", set a "global" flag (more likely a class variable) and "remember" where the hit occured. On the following turns, guess the neighboring squares in some predetermined order (say north, south, east, west) until another hit is found. Then set another flag and on the next turn guess in the same direction as the second hit. The initial flag should only be reset when all three hits are found. This should fix the problem that a subsequent miss causes the computer AI to start guessing randomly again.
Related
Sorry for lack of details, I made a few edits.
I've got a problem in my java class that I can't wrap my head around.
I need to make a program that draws random numbers which are formations in a 10 round competition.
In this competition judges score you based on the formations you draw.
You can only have 5 or 6 points per round. You can only have 6 points if you draw 3 - 2 point formations.
The range of formations is 1-38.
Formations 1-16 are worth 1 point.
The rest are worth 2 points.
I tried this.
int formation;
int points;
int round;
boolean isOnePoint;
int maneuvers = 0;
public void main(String args[]){
while(points < 5){
getRandomFormation();
if(points < 5 && isOnePoint){
points++;
}
else if(points == 4 && !isOnePoint && maneuvers < 3){
points +=2;
} maneuvers++;
}
}
public void getRandomFormation(){
formation = rand.nextInt(38);
if(formation < 17){ isOnePoint = true; } else isOnePoint = false;
}
The problem I am facing is with this code that I came up with I am still able to get something like:
// example of output round with points recieved
Round 6: 1, 2, 1, 2
The problem however, is that you cannot get 6 points with anything other than getting three random 2 point combinations...
What am I overlooking / missing in my code to be able to get it right?
Based off what you have posted you seem to only want three numeric values.
Change your while(points<5) to while(maneuver<3) and increment maneuver at the end of the loop.
That would be the quickest way to solve your problem. The logic in your if statements are quite convoluted. You only check to see the number of maneuvers in the second if statement which means that you can still meet the parameters for the first if.
for(int i=0;i<3;i++){
getRandomFormation();
if(isOnePoint)
points++;
else
points +=2;
}
I am new in this page, it hope get to some help, basically I am doing a minesweeper game on Java but it have a problem with a function: discover the region with no mines and no numbers like in the game on windows, when you click in one place and all withe cells appear. I tried make recursive but I can't, some help?
Sorry for the code, the original is in spanish but i tried make a pseudocode:
Matriz = multidimensional Array (the minesweeper)
min and max returns the index min and max to iterate (8 sorroud cells)
private void discoverWitheCell(int x, int y) {
if(matriz[x][y].getdiscovered() == false){
matriz[x][y].setDiscovered(true);
}
else{
if(matriz[x][y].getNumberOfMinesArround() == 0){
for(int i=min(x);i<max(x);i++)
for(int j=min(y);j<max(y);j++)
discoverWhiteCell(i,j);
}
}
}
There's not a lot of code here but I feel like you're coming at it backwards.
Sorry, I'm not a Java speaker so I'm guessing at some of the syntax. Note that this can go out of bounds--personally, I would add a layer of empty cells around my map so I never need to concern myself with bounds checking.
private void ClickSquare(int x, int y)
{
// Did the user click an already exposed square? If so, ignore
if (matriz[x][y].getDiscovered()) return;
matriz[x][y].SetDiscovered(true);
if (matriz[x][y].getNumberOfMinesAround != 0) return;
// If empty, click all the neighbors
for (int xloop = x - 1; xloop <= x + 1; xloop++)
for (int yloop = y - 1; yloop <= y + 1; yloop++)
ClickSquare(xloop, yloop);
}
I believe you have the discovered test messed up and your version appears to be able to go into infinite (until the stack overflows) recursion as if the neighbor is also zero it will come back to the original cell. My version stops this recursion by only processing a cell if it hasn't already been processed.
Here is the algorithm (not working) Please let me know where the error is
Thanks
private void checkSouth(Location point, int player) {
//Loop through everything south
boolean isthereAnOppositePlayer=false;
int oppositePlayer=0;
//Set opposite player
if (player==1) {
oppositePlayer=2;
}else{
oppositePlayer=1;
}
for (int i = point.getVertical(); i < 8; i++) {
//Create a location point with the current location being compared
MyLocation locationBeingChecked= new MyLocation();
locationBeingChecked.setHorizontal(point.getHorizontal());
locationBeingChecked.setVertical(i);
int value = board[locationBeingChecked.getVertical()][locationBeingChecked.getHorizontal()];
//If the first checked is the opposite player
if (value==oppositePlayer) {
//Then potential to evaluate more
isthereAnOppositePlayer=true;
}
//If it isn't an opposite player, then break
if(!isthereAnOppositePlayer && value!=0){
break;
}
//If another of the player's piece found or 0, then end
if (isthereAnOppositePlayer && value==player || isthereAnOppositePlayer && value==0) {
break;
//end
}
//Add to number of players to flip
if(isthereAnOppositePlayer && value==oppositePlayer && value!=0){
//add to array
addToPiecesToTurn(locationBeingChecked);
}
}
}
It looks like the locations that got rotated back to the other player are the exact same as those rotated during the first move. I would guess that the array being populated by addToPiecesToTurn is perhaps not being cleared out between each move, so all the previous locations are still in there.
If you are storing the pieces to be turned in an ArrayList, you can use the clear() method to erase the contents of the collection between each turn.
Another possible problem is that you are checking for the opposite player, and then instantly beginning to populate addToPiecesToTurn. However, the pieces in that direction are not necessarily valid to be rotated unless they are "sandwiched" in by a second location containing the current player's piece. I don't think your code is properly checking for that case; when that happens, you'll want to somehow skip flipping those pieces to the other player, such as clearing out the array of piecesToTurn.
Edit: Looking at your current solution where you are implementing every direction separately, you are going to have a lot of duplicated code. If you think about what it means to walk along a certain direction, you can think of it as adjusting the x/y value by a "step" amount. The step amount could be -1 for backwards, 0 for no move, or 1 for forwards. Then you could create a single method that handles all directions without duplicating the logic:
private void checkDirection(Location point, int player, int yStep, int xStep) {
int x = point.getHorizontal() + xStep;
int y = point.getVertical() + yStep;
MyLocation locationBeingChecked = new MyLocation();
locationBeingChecked.setHorizontal(x);
locationBeingChecked.setVertical(y);
while (isValid(locationBeingChecked)) {
// do the logic here
x += xStep;
y += yStep;
locationBeingChecked = new MyLocation();
locationBeingChecked.setHorizontal(x);
locationBeingChecked.setVertical(y);
}
}
You would need to implement isValid to check that the location is valid, i.e., in the board. Then you could call this method for each direction:
// north
checkDirection(curPoint, curPlayer, -1, 0);
// north-east
checkDirection(curPoint, curPlayer, -1, 1);
// east
checkDirection(curPoint, curPlayer, 0, 1);
// etc
This is the sort of problem that is ripe for some unit testing. You could very easily set up a board, play a move, and validate the answer, and the test results would give plenty of insight into where your expectations and reality diverge.
why didn't you use a 2d array ?
each cell would contain an enum : EMPTY, PLAYER_1, PLAYER_2 .
then, in order to go over the cells, you simply use loops for each direction.
for example, upon clicking on a cell , checking to the right would be:
for(int x=pressedLocation.x+1;x<cells[pressedLocation.y].length;++x)
{
Cell cell=cells[pressedLocation.y][x];
if(cell==EMPTY||cell==currentPlayerCell)
break;
cells[pressedLocation.y][x]=currentPlayerCell;
}
checking from top to bottom would be:
for(int y=pressedLocation.y+1;y<cells.length;++y)
{
Cell cell=cells[y][pressedLocation.x];
if(cell==EMPTY||cell==currentPlayerCell)
break;
cells[y][pressedLocation.x]=currentPlayerCell;
}
I dont know why but whenever you or the computer gets a hit in my game, the grids both get updated.
The player and computer grids are multidimensional arrays of '-' chars and when you get a hit it changes to 'x'. In the game loop, their is a player grid and computer grid and I update them each seperately at different times but when they get printed their both the same. Can someone help? Sorry I'm new to programming
public class Game{
public static void main(String[] args){
Grid grid = new Grid();
Computer computer = new Computer();
Player player = new Player();
String playerGuess;
player.setPlayerShips();
computer.setComputerShips();
char[][] playerGrid= Grid.gridArray;
char[][] computerGrid = Grid.gridArray;
System.out.println("Welcome to BATTLESHIP");
System.out.println("You are to sink your opponents 3 ships, each 3 units in length.");
System.out.println("The ships can be both vertical and horizontal.");
System.out.println("Enter your coordinate guess in the form A1, B5, F6, etc.");
System.out.println("Since the grid is 7x7, coordinates go from A1-G7.");
System.out.println("Letters are vertical, numbers are horizontal.");
System.out.println("You will also have 3 ships placed randomly, which the computer will also try to guess.");
System.out.println("During the game, enter exit if you would like to quit.");
System.out.println();
while(true){
System.out.println("Your Grid");
grid.printGrid(playerGrid);
System.out.println("Opponent's Grid");
grid.printGrid(computerGrid);
System.out.println();
playerGuess=player.getGuess();
if(playerGuess.equals("exit")){
break;
}else{
playerGuess=grid.convert(playerGuess);
}
player.setFirstCo(playerGuess);
player.setSecondCo(playerGuess);
System.out.println();
if(player.isHit(player.firstCo, player.secondCo)){
player.addHits(player.firstCo, player.secondCo);
System.out.println("Hit!");
System.out.println();
computerGrid=Grid.newGrid(computerGrid,player.firstCo,player.secondCo);
}else{
System.out.println("Miss.");
System.out.println();
}
if(player.hasWon()){
System.out.println("Congratulations, you have sunk all your opponents ships!");
break;
}
computer.guess=computer.getGuess();
computer.lastGuess=computer.guess;
if(computer.isHit(computer.guess[0],computer.guess[1])){
computer.addHits(computer.guess[0],computer.guess[1]);
System.out.println("Computer has hit!");
System.out.println();
playerGrid=grid.newGrid(playerGrid, computer.guess[0], computer.guess[1]);
if(computer.hasWon()){
System.out.println("Computer has sunk all your ships! You lose.");
break;
}
}else{
System.out.println("Computer has missed.");
System.out.println();
}
}
}
}
i got the grids to print separately but theres something wrong with my place ships method. Can someone take a look at it? It's suppose to choose random x,y coordinates(3 points for each ship) and do this for 3 ships. It places 4 points in a row sometimes and no other ships(I think the ships are just overlapping, but I tried to put a fix in). Anyways, thanks in advance if you can help.
//set player ships coordinates, can be numbers from 0-6
public static void setPlayerShips(){
int randX, randY;
int direction; //will be random int 0-1, determines direction ship will extend(up/down, left/right)
randX=(int)(Math.random()*7);
randY=(int)(Math.random()*7);
direction=(int)(Math.random()*2);
playerShip1[0]=randX;
playerShip1[1]=randY;
if(direction==0){//extend upwards or downwards 2 units(y values change, x stays the same)
playerShip1[2]=randX;
playerShip1[4]=randX;
if(randY>3){//if y value is greater than 3, has to extend down or it wont fit
playerShip1[3]=randY-1;
playerShip1[5]=randY-2;
}else if(randY<2){//if y value is less than 2, has to extend up or it wont fit
playerShip1[3]=randY+1;
playerShip1[5]=randY+2;
}else{//if direction doesnt matter, just extend upwards
playerShip1[3]=randY+1;
playerShip1[5]=randY+2;
}
}else if(direction==1){//extends left or right 2 units(y values stay the same, x changes)
playerShip1[3]=randY;
playerShip1[5]=randY;
if(randX>3){//if x is greater than 3, must extend left or it wont fit
playerShip1[2]=randX-1;
playerShip1[4]=randX-2;
}else if(randX<2){//if x is less than 2, must extend right or it wont fit
playerShip1[2]=randX+1;
playerShip1[4]=randX+2;
}else{//if direction doesnt matter, just extend right
playerShip1[2]=randX+1;
playerShip1[4]=randX+2;
}
}
//do same for both other ships, do quick checks to make sure original coordinates arent the same
do{
randX=(int)(Math.random()*7);
randY=(int)(Math.random()*7);
}while(randX==playerShip1[0] && randY==playerShip1[1]);
direction=(int)(Math.random()*2);
playerShip2[0]=randX;
playerShip2[1]=randY;
if(direction==0){
playerShip2[2]=randX;
playerShip2[4]=randX;
if(randY>3){
playerShip2[3]=randY-1;
playerShip2[5]=randY-2;
}else if(randY<2){
playerShip2[3]=randY+1;
playerShip2[5]=randY+2;
}else{
playerShip2[3]=randY+1;
playerShip2[5]=randY+2;
}
}else if(direction==1){
playerShip2[3]=randY;
playerShip2[5]=randY;
if(randX>3){
playerShip2[2]=randX-1;
playerShip2[4]=randX-2;
}else if(randX<2){
playerShip2[2]=randX+1;
playerShip2[4]=randX+2;
}else{
playerShip2[2]=randX+1;
playerShip2[4]=randX+2;
}
}
do{
randX=(int)(Math.random()*7);
randY=(int)(Math.random()*7);
}while((randX==playerShip1[0]&& randY==playerShip1[1])&&(randX==playerShip2[0] && randY==playerShip2[1]));
direction=(int)(Math.random()*2);
playerShip3[0]=randX;
playerShip3[1]=randY;
if(direction==0){
playerShip3[2]=randX;
playerShip3[4]=randX;
if(randY>3){
playerShip3[3]=randY-1;
playerShip3[5]=randY-2;
}else if(randY<2){
playerShip3[3]=randY+1;
playerShip3[5]=randY+2;
}else{
playerShip3[3]=randY+1;
playerShip3[5]=randY+2;
}
}else if(direction==1){
playerShip3[3]=randY;
playerShip3[5]=randY;
if(randX>3){
playerShip3[2]=randX-1;
playerShip3[4]=randX-2;
}else if(randX<2){
playerShip3[2]=randX+1;
playerShip3[4]=randX+2;
}else{
playerShip3[2]=randX+1;
playerShip3[4]=randX+2;
}
}
}
This is the problem:
char[][] playerGrid= Grid.gridArray;
char[][] computerGrid = Grid.gridArray;
You've only actually got a single char[][] object here. Both variables refer to the same object... any updates made to that object will be visible via both variables.
It looks like Grid.gridArray is actually a static variable, which is another problem. You almost certainly want to make it an instance variable... and then create two instances of Grid rather than just one.
Basically, I would take a step back and work out what an instance of Grid is meant to represent. Do you really need to expose the gridArray variable at all? Why can a grid not print itself?
This game is a 3D version of tic-tac-toe with 3 boards. Each position on the boards are made up of [x,y,z] coordinates. x is the number of the board and y,z are the coordinates on that board. I'm trying to define a method that only lets you choose from the first board for the first 9 turns of the game, but am having problems. Here is the code.
public void set(Position p, int v) throws IOException {
if (board[p.x][p.y][p.z]!= 0) throw new IOException("Position taken");
board[p.x][p.y][p.z] = v;
I'm not sure what to add to this method to only allow moves on board one for the first 9 turns. I have tried using a counter such as an if statement such as...
public void set(Position p, int v) throws IOException {
int counter = 0;
if (board[p.x][p.y][p.z]!= 0) throw new IOException("Position taken");
board[p.x][p.y][p.z] = v;
while (counter < 10) {
if (p.x != 1) throw new IOException("Invalid Move");
}
When I tried this and other manipulated versions of this same method I kept getting the "Invalid Move" error everytime, even when p.x was equal to 1. Any suggestions on how to loop this or any other ways to accomplish this?
You really ought to do your own learning rather than asking us to do your homework assignments for you. I am not going to answer your question but I will point out that there are several huge problems with your code:
1: counter is scoped within the set method and will therefore always be set zero at the start of that method even if this is the 20th turn.
2: counter is never incremented and will therefore always have its initial value.
3: Arrays in Java start at zero not one.
4: IOException is not a suitable exception for this method to throw as it is not doing any IO.
[...] only lets you choose from the first board for the first 9 turns of the game [...]
Note that the first element of an array has index 0, so you may want to change
p.x != 1
to
p.x != 0
Also, I think you have a problem with:
int counter = 0;
// ...
// ...
while (counter < 10) {
if (p.x != 1)
throw new IOException("Invalid Move");
}
as it will go into an infinite loop.
You probably want to have counter as a member variable and do counter++ in the move method (if the move was successful).