Text based battleship game printing two of the same grids - Java - java

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?

Related

How do I count the number of bombs in my Minesweeper code?

So well I tried creating a simpler Minesweeper game and I encountered one main problem..
I am not able to count the number of bombs and print it in a JTextField
Any ideas as to how to count these as I'm setting a random value to check whether they are a bomb
Tried counting it in the ActionListener but the bombs were counted only after the button was clicked.
if(e.getSource()==b[i][j])
{
b[i][j].setVisible(false);
tf[i][j].setVisible(true);
int r1 = rand.nextInt(6);
if(r1>1)
{
tf[i][j].setText("Safe");
tf[i][j].setBackground(Color.green);
}
else
{ count++;
tf[i][j].setText("Bomb");
tf[i][j].setBackground(Color.red);
f.setVisible(false);
restart.setVisible(true);
}
}
As I understand you decide if the the tile will be a bomb in the run-time using a random generator. Doing this you can't really know how many mines are in your game. I think you should decide the number of mines at the beginning of the game and randomly place them to your game board (you can choose the number according to a difficulty level).
EDIT
You can create a list with some random points that contain the mines
int numOfMines = 10;
int rows=5,columns=5;
ArrayList listWithMines = new ArrayList();
while(listWithMines.size()<numOfMines) {
int randRow = random.nextInt(rows);
int randCol = random.nextInt(columns);
Point point = new Point(randRow, randCol);
if(listWithMines.contains(point))
continue;
else
listWithMines.add(point);
}
This list now contains the Points that have the mines.
You can check if Point(x,y) has a mine like this:
if(listWithMines.contains(new Point(1, 2))) {...}
Instead of a list you can use a 2D array, store a boolean value (or int if you store more states) and make a loop until you place 10 mines. You should keep a counter(placedMines like the list.size()) of the mines you placed and make sure you don't add a mine to a tile that has already a mine and you increase the counter(placedMines) until it reaches the numOfMines.

Making a program repeat within itself, but you can't make a method(?): Java

I have a project for my computer science class and we're making battleship. Part of the program is that we have make sure that the piece the player puts down does not go off of the board.
I've made a method to check to see whether it goes off the board:
private static boolean test(String s, int row, int column,int spaces)
{
if(s.equals("right")&&column+5<=10)
{
return true;
}
if(s.equals("up")&&row-spaces>=0)
{
return true;
}
if(s.equals("left")&&column-spaces>=0)
{
return true;
}
if(s.equals("Down")&&row+spaces<=10)
{
return true;
}
return false;
}
But once I've gotten it to print out an error message, I'm not sure how to make it so that the program can re-recieve the new position for the piece, without putting an if statement in and if statement in an if statement (and on and on), because you need to check the new position to make sure it doesn't go off of the board.
Here is the part where I get the position of the playing piece (although I don't think you need it)
Scanner sonic= new Scanner(System.in);
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
int beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
int begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
String direction = sonic.next();
And here's one of the if statements that I use to check/place the pieces
if(direction.equals("left")&&test("left",beginrow,begincolumn,5))
{
for(int i = beginrow; i>beginrow-5; i--)
{
battleship[begincolumn-1][i-1] = ('a');
}
}
else if(!test("left",beginrow,begincolumn,5))
{
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
}
This may be a duplicate, but I didn't know how to reword my search to find what I wanted. (So if anyone could direct me to the right article, that'd be nice as well)
What you should do is split your code appropriately into methods and call that methods repeatedly until your program is satisfied with the outcome.
For example:
create a method startGame() which has the job call methods getting user input until satisfied
make a method to request the user to input all the different ships and other required data
That might look something like
public void startGame() {
// do some setup
while(!requestShipInput()) { // request ship data until the data is valid
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
}
// do some more ship setup
// get the actual playing started
}
public boolean requestShipInput() {
Scanner sonic= new Scanner(System.in);
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
int beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
int begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
String direction = sonic.next();
if(direction.equals("left")&&test("left",beginrow,begincolumn,5)) {
for(int i = beginrow; i>beginrow-5; i--) {
battleship[begincolumn-1][i-1] = ('a');
}
return true; // valid ship data
}
return false; // invalid ship data
}
As a first step, separate input validation from taking the action based on that input - you already have the validation logic in a separate function, so this is easy. Then figure out what needs to be done in case of invalid input - in your case, you need to ask for new input until you get a valid position:
do {
System.out.println("Please input the row where you want the aircraft carrier (5 spaces) to begin: ");
beginrow = sonic.nextInt();
System.out.println("Please input the column where you want the aircraft carrier (5 spaces) to begin: ");
begincolumn = sonic.nextInt();
System.out.print("Please input what direction (up, down, left, right) \nyou want your battle ship to face, making sure it doesn't go off of the board.");
direction = sonic.next();
} while (!test(direction, beginrow, begincolumn, 5))
After that, you know you've got a valid position.
My next step would probably be to group the information required to describe a ship on the board (i.e. beginrow,begincolumn,direction, probably also size) in a separate Object - possibly named Ship.
I think you could pretty naturally use recursion here:
public void getInput() {
// scanner code to get input
if (!test("left",beginrow,begincolumn,5)) { // test failed
getInput()
return
}
// test succeeded, continue
}
You already have something to the limits of you board? If you execute the check first, you don't need to execute a cascade of if-else
if(!test(direction,beginrow,begincolumn,size))
{
System.out.println(" ");
System.out.println("*****ERROR: your piece goes off the board, please re-enter your position and direction*****");
} else {
// check for collision with already placed ships
}
Keep in mind that there is a chance to combine up/down and left/right. The calculation rules are nearly the same and you only have to decide if you have to look to the one or the other direction.

How to choose a spesific object from ArrayList

We are making a simple rpg game. Right now we have a class called battleground, where the player can fight monsters. We use a random generator to pick out a random monster when we start the fight. In the terminal/main method, we have commands like "attack" and "run", that will either do damage to the random monster, or make the player leave/quit the game. Right now, we are trying to add a command called "attack scariest", which will let the player fight against the hardest monster with the most damage(there are three to choose from in our main). We need a method to choose a specific object from the ArrayList monsters, based on damage. Does anyone have tips on how we do that?
This is our code in the Battleground class that starts the game:
public void startBattle() {
printWelcomeMessage();
boolean finished = false;
this.currentMonster = getRandomMonster();
if(this.currentMonster == null) {
return;
}
System.out.println("A random monster is chosen for you. Prepare to meet the mighty " + this.currentMonster.getName());
System.out.println("\n-------- Player Stats ---------");
System.out.println(this.player);
while(!finished && monsters.size() > 0) {
ArrayList<String> commands = reader.getInput();
if(isValidCommand(commands)) {
if(commands.contains("quit") && !this.currentMonster.isDead()){
System.out.println("You can't quit the game in the middle of a fight!");
}else if(commands.contains("quit") && this.currentMonster.isDead()) {
finished = true;
System.out.println();
printFinalStats();
System.out.println();
System.out.println("You have left the arena, and the game has ended.");
System.out.println();
}
if(commands.contains("run")) {
finished = true;
System.out.println("You are a coward and you lose 50 gold pieces...\n");
this.player.changeGold(-50);
printFinalStats();
System.out.println("\nThanks for playing...");
}else if(commands.contains("drink") && !this.currentMonster.isDead()){
Potion potion = (Potion) player.findItem(commands.get(1));
player.drinkPotion(potion);
}else if(commands.contains("equip") && !this.currentMonster.isDead()){
Weapons weapon = (Weapons) player.findItem(commands.get(1));
player.useWeapon(weapon);
} else if(commands.contains("attack") && !this.currentMonster.isDead()) {
if(this.player.attack(this.currentMonster)) {
playerWon();
if(this.monsters.size() > 0) {
System.out.println("\nThere are " + this.monsters.size() + " more monsters to beat\nType \"attack\" if you want to attack another monster, or \"quit\" if you want to end the game.");
} else {
System.out.println("\n\n#### Congratulations ####\nYou have beaten every single monster in the game. You are a true champion!");
printFinalStats();
finished = true;
}
} else if(this.currentMonster.attack(this.player)) {
printLosingMessage();
finished = true;
}
} else if(commands.contains("attack") && this.currentMonster.isDead() && this.monsters.size() > 0) {
this.currentMonster = getRandomMonster();
printContinueMessage(this.player, this.currentMonster);
this.player.changeHealth(50);
}
} else {
System.out.println("Please write a valid command. Valid commands are:");
printCommands();
}
}
}
This is the ArrayList of monsters in the main class:
Monster beelzebub = new Monster("Beelzebub", 60);
Monster witch = new Monster("Witch", 40);
Monster ogre = new Monster("Ogre", 80);
ArrayList<Monster> monsters = new ArrayList<>();
monsters.add(beelzebub);
monsters.add(witch);
monsters.add(ogre);
Battleground battleground = new Battleground(monsters, player, reader);
battleground.startBattle();
We appreciate any help!
Replace the ArrayList with a SortedSet or a PriorityQueue. Implement a Comparator for Monster class. Then just pick the first element of the monsters collection.
I can't see your Monster class but I'm assuming the second variable to the constructor is the 'damage level'? If not, ideally this should be something that belongs to each monster so you should be setting it somewhere.
So you have the array Monsters which has all the monsters with various levels of damage already. There are a lot of ways you could do this, but a simple way (given you only have 3 monsters) is to just iterate over your monsters and keep track of the monster with the highest damage, and then return that monster.
For example:
Monster findHardestMonster(ArrayList<Monster> monsters)
{
//Set to the first monster in the list ASSUMING you have
//at least one monster
Monster highestDamageMonster = monsters.get(0);
//Go through every monster in your array
for (Monster monster : monsters)
{
//record the highest so far
if(monster.getDamage() > highestDamageMonster.getDamage())
highestDamageMonster = monster;
}
}
//return it
return highestDamageMonster;
}
If you plan on removing monsters frequently (say, when they're defeated), it might be worth as another poster suggested to use a priority queue or some sort of ordered collection whereby the ordering is based on damage level instead. Then you won't have to iterate over the monsters every time to find the one with max damage.
for(int i=0;i<monsters.size()-1;i++){
Monster m=monsters.get(i);
Monster m2=monsters.get(i+1);
if(m.getDamage()<m2.getDamage()){
Monster rightMonster=m2;
}else{
Monster rightMonster=m;
}
}
This should work if i am not mistaken. i havent tried the code but maybe it gives you hints
The stream operations you can perform on the list will let you do this easily, by using max with a Comparator that ranks the monsters according to damage.
Comparator<Monster> orderByDamage =
(m1, m2) -> m1.getDamage() < m2.getDamage() ? -1
: m1.getDamage() == m2.getDamage() ? 0 : 1;
What this does is compare the damage of two monsters (m1 and m2). If the damage of m1 is less than that of m2, a negative number is returned. If they are equal, zero is returned and if m1 has more damage than m2, a positive number is returned.
Note: if getDamage() returns an Integer and not an int, replace m1.getDamage() == m2.getDamage() with Objects.equals(m1.getDamage(), m2.getDamage()).
Use this with your list to get the monster with the most damage:
Optional<Monster> scariest = monsters.stream().max(orderByDamage);
Note the return type, which is Optional<Monster>. If the list is not empty, the Optional will contain the scariest monster; if the list is empty, the optional will be empty - this will help avoid a NullPointerException if all monsters have been removed.
scariest.ifPresent(monster -> /* do something to the monster */);
You can also use this approach to get the monster with the least damage:
Optional<Monster> leastScary = monsters.stream().min(orderByDamage);

Java Method not working constantly

I have a text based game that I am making. It is a RPG style where the user is given options linked to numbers and they have to choose a number. Now my problem is that when running the program. A certain method, Decision(), only works certain times. The method is in a superClass while it is being called in the subclass. in the subclass, It works the first time, but not necessarily the second. Also, when I copy the decision method from the superclass into the subclass its starts working, but the next time it is called, it stops. Here is what I've tried and the results. I've included the decision method and where it is being called.
Decision Method:
public int decision(String question, int length, String[] choices){
int[] numbers = new int[length];
int iterator = 1;
for(int i = 0; i < length; i++){
numbers[i] = iterator;
iterator++;
}
boolean done = false;
while(!done){
//print("Test");
print("");
print(question);
String options = "";
for(int i = 0; i < numbers.length; i++){
options = (options + numbers[i] + " - " + choices[i] + " ");
}
print(options);
boolean univSet = true;
int entry = 1;
while(univSet){
if(univInt != 0){
univSet = false;
entry = univInt;
univInt = 0;
//print("testing");
}
}
if(entry == 23){
help();
}else{
for(int i = 0; i < numbers.length; i++){
if(entry == numbers[i]){
done = true;
univInt = 0;
return entry;
}
}
print("Invalid Number, Try again");
print("");
univInt = 0;
}
}
return (Integer) null;
}
Chapter1 Class (Where it's being called:
public class Chapter1 extends Story implements Serializable {
Player player;
public Chapter1(Player player){
this.player = player;
}
public void engage() {
// TODO Auto-generated method stub
player.chapter = 1;
save(player.name);
sPrint("Welcome to Chapter 1");
print("You wake up in a lighted room with white walls.\nA fresh breeze is coming through the window yet the air smells rotten.");
print("You jolt up suddenly. You don't remember anything about how you got here. You've even forgotten who you are.");
print("You look down at your white shirt, there is a streak of blood across the top.\nYou are wearing a nametag that says: " + player.name + ".");
print("You're sitting in a chair but there are no restraints. You decide to get up and look around");
cur = decision("What do you do?", 2, new String[]{"Try the door", "Look out the window"});
print(cur + "");
if(cur == 1){
print("You walk over to the door and try and open it, it is unlocked.\nYou walk through and are welcomed by a cold and poorly lit hallway");
}else{
print("You walk to the window and look outside. You see a huge barren field. You can make out a humanoid like structure.\nYou call out yet the figure doesn't move.");
print("You decide to try the door. It's unlocked so you walk through into a cold dimly lit hallway.");
}
print("You see a dull knife on the floor as well as a door on the end of the hallway");
cur = decision("What do you do?", 2, new String[]{"Go to the door", "Take the knife"});
if(cur == 2){
print("You pick up the knife.");
addWeapon("Kitchen Knife", player);
}else{
print("You walk down the hallway to the door when suddenly the door opens and out comes a zombie.\nIt Lunges for your shoulder. You are caught by surprise and it bites into your skin and you are infected");
gameOver();
}
print("You continue to walk down the hall when suddenly a hideous creature lunges out from the door.\nYou jump back and prepare yourself for a battle.");
battle("Zombie", 5, 2, player);
sPrint("I see that you have succeeded in your first encounter with the undead.\nI congratulate you but you have a long way to go. Remember, I am your only way to learning about your past. \nNow, make your way down to the bottom of the tower. I will help you where I see fit along the way.");
print("You look around and see that the lights have brightened up. The zombie has been mutilated by your Kitchen Knife. \nYou don't know where the voice came from but you are scared. Behind the zombie's original hiding spot you see a staircase.\nYou follow it down, onto what seems to be..the 11th floor.");
print("");
print("Please input 'complete' to continue");
pause();
sPrint("Chapter 1 complete");
}
Now in this class, engage() is being called to run this chapter. And decision is being called where you see it, as well as in the battle() method(the battle method loops a couple times and decision() is called every loop.
Originally, both Decision and battle are in the superclass, but not in the sub class. This results in the first decision method in the class to be called, but not the second. In the second, it stops at the loop checking the value of univInt.
When I put the decision method into the sub class, It passes the first two but it fails to get past the first one in the battle method for the same reason.
When I put both the decision and battle method into the sub class, it has the same result as just putting decision.
Finally if I put battle in the sub class but not decision it only passes the first two again.
In the project I have one variable named cur that holds the integer value of whatever decision returns. I reuse it for every decision. I don't know if that has anything to do with it. This really doesn't make sense to me how whether the methods are in the same class, or inherited would matter at all if they are the same method.
I am ready to clarify anything and I hope someone is able to understand what is going wrong.
EDIT:
univInt is being set to another number other than 0 outside of decision. thats why it works some times. It is a swing class and a method in a superclass sets univInt to whatever is in a TextField when a button is pressed so with that while loop I try to constantly check to see univInt has been changed from 0
It seems like your "univInt" is a class member, not a local variable, and you do not reinitialize it when entering the function. Thus it won't be changed back to allow the program to enter the if-statement you mention.

Text based battleship enemy AI not working as wanted - Java

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.

Categories