Can someone help me figure out how to make this code play a computer instead of
ID: 3769796 • Letter: C
Question
Can someone help me figure out how to make this code play a computer instead of a person and to use the arrow keys. Needs to be in basic c+code and runs in code blocks 10.05 #include #include string> #include #include curses.h> using namespace std: const int BOARD WIDTH = 10: const int BOARD HEIGHT = 10; const int SHIP TYPES = 5; const char isWATER-247;WASCII Character Code const char isHIT = "X": const char isSHIP-S; struct POINT A location on the grid defined /by X(horizontal) Y(vertical) coordinates int X int Y struct SHIP Ship namoe string name; Total points on the grid int length; /Coordinates of those points POINT onGrid[5]: /0-4 max length of biggest ship //Whether or not those points are a "hit bool hitFlag[5]: ship[SHIP TYPES] struct PLAYER char grid[BOARD WIDTHI[BOARD HEIGHT]: player(3];/Ignore player 0, just using player's 1 & 2; player 2 is computer enum DIRECTION HORIZONTAL,VERTICAL struct PLACESHIPS DIRECTION direction; SHIP shipType; placeShip /Functions void LoadShips0: void ResetBoardO; void DrawBoard int); PLACESHIPS UserlnputShipPlacemento PLACESHIPS RandomShipPlacementO; int maino LoadShips0: ResetBoardo: "PLACE SHIPS" phase of game /Loop through each ship type to place int aplyr = 1 ;Explanation / Answer
import java.util.Random;
import java.util.Scanner;
public class battleShip {
public static void main(String[] args) {
int[][] board = new int[5][5];
int[][] ships = new int[3][2];
int[] shoot = new int[2];
int attempts=0,
shotHit=0;
initBoard(board);
initShips(ships);
System.out.println();
do{
showBoard(board);
shoot(shoot);
attempts++;
if(hit(shoot,ships)){
hint(shoot,ships,attempts);
shotHit++;
}
else
hint(shoot,ships,attempts);
changeboard(shoot,ships,board);
}while(shotHit!=3);
System.out.println(" Battleship Java game finished! You hit 3 ships in "+attempts+" attempts");
showBoard(board);
}
public static void initBoard(int[][] board){
for(int row=0 ; row < 5 ; row++ )
for(int column=0 ; column < 5 ; column++ )
board[row][column]=-1;
}
public static void showBoard(int[][] board){
System.out.println(" 1 2 3 4 5");
System.out.println();
for(int row=0 ; row < 5 ; row++ ){
System.out.print((row+1)+"");
for(int column=0 ; column < 5 ; column++ ){
if(board[row][column]==-1){
System.out.print(" "+"~");
}else if(board[row][column]==0){
System.out.print(" "+"*");
}else if(board[row][column]==1){
System.out.print(" "+"X");
}
}
System.out.println();
}
}
public static void initShips(int[][] ships){
Random random = new Random();
for(int ship=0 ; ship < 3 ; ship++){
ships[ship][0]=random.nextInt(5);
ships[ship][1]=random.nextInt(5);
//let's check if that shot was already tried
//if it was, just finish the do...while when a new pair was randomly selected
for(int last=0 ; last < ship ; last++){
if( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) )
do{
ships[ship][0]=random.nextInt(5);
ships[ship][1]=random.nextInt(5);
}while( (ships[ship][0] == ships[last][0])&&(ships[ship][1] == ships[last][1]) );
}
}
}
public static void shoot(int[] shoot){
Scanner input = new Scanner(System.in);
System.out.print("Row: ");
shoot[0] = input.nextInt();
shoot[0]--;
System.out.print("Column: ");
shoot[1] = input.nextInt();
shoot[1]--;
}
public static boolean hit(int[] shoot, int[][] ships){
for(int ship=0 ; ship<ships.length ; ship++){
if( shoot[0]==ships[ship][0] && shoot[1]==ships[ship][1]){
System.out.printf("You hit a ship located in (%d,%d) ",shoot[0]+1,shoot[1]+1);
return true;
}
}
return false;
}
public static void hint(int[] shoot, int[][] ships, int attempt){
int row=0,
column=0;
for(int line=0 ; line < ships.length ; line++){
if(ships[line][0]==shoot[0])
row++;
if(ships[line][1]==shoot[1])
column++;
}
System.out.printf(" Hint %d: Row %d -> %d ships " +
"Column %d -> %d ships ",attempt,shoot[0]+1,row,shoot[1]+1,column);
}
public static void changeboard(int[] shoot, int[][] ships, int[][] board){
if(hit(shoot,ships))
board[shoot[0]][shoot[1]]=1;
else
board[shoot[0]][shoot[1]]=0;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.