In the game of battleships, the game play area is a square grid containing ships
ID: 3796849 • Letter: I
Question
In the game of battleships, the game play area is a square grid containing ships. Players take it in turns to guess the location of the ships until the battleship is sunk.
A simple solution has been provided that has the basics of rendering a grid and asking for input. It does not look very pretty.
Your assignment is to improve on the rendering and complete the input part of the gameplay.
Improve the rendering so that each grid square is represented by a box created out of characters.
e.g.
#---#
| |
#---#
Improve the rendering so that each ship type is in a different color. Ships should be visible, not hidden from view. Yes, that’s a bad game but we are just using this to test our algorithms and design.
e.g.
#---#
| S |
#---#
Improve the rendering so that the grid has horizontal labels (A through J) and vertical labels (1 through 10)
Parse the input to get the guess in the form LetterNumber e.g. B7, G10, A1. You will find String.SubString and Int32.TryParse to be useful API calls. Invalid input should cause the input to be asked again – it should not be possible to crash your program with bad input
When you have a valid input mark the grid location as used and then render an X in the square to indicate it has been used. The X should be Red if it was a hit on a ship.
e.g.
Miss Hit
#---# #---#
| X | | X |
#---# #---#
You do not need to determine end of game conditions, validate repeated hits or any gameplay
The following is the code:
using System;
namespace BattleshipSimple
{
class Program
{
private static readonly char[,] Grid = new char[,]
{
{'.', '.', '.', '.', 'S', 'S', 'S', '.', '.', '.'},
{'P', 'P', '.', '.', '.', '.', '.', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'P'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', 'P'},
{'.', '.', 'A', 'A', 'A', 'A', 'A', '.', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', 'B', '.', '.'},
{'.', 'S', '.', '.', '.', '.', '.', 'B', '.', '.'},
{'.', 'S', '.', '.', '.', '.', '.', 'B', 'P', 'P'},
{'.', 'S', '.', '.', '.', '.', '.', 'B', '.', '.'},
{'.', '.', '.', '.', '.', '.', '.', '.', '.', '.'},
};
static void Main(string[] args)
{
while (true)
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
Console.Write(Grid[i, j]);
}
Console.WriteLine();
}
Console.WriteLine("Enter your guess:");
var guess = Console.ReadLine();
}
}
}
}
Create a program to print a battleship grid to the console and mark squares as destroyed
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
Notes:
You may not modify the char array to store the extra characters to print. The array should only store information about the gameboard and the extra output should be generated by your code.
Your grid rendering must display the ships. At this stage we are just testing parts of the game so we don’t need them hidden from the user. (and it makes it much harder for one of your users to grade!)
You do not need to determine end of game conditions, validate repeated hits or any gameplay however feel free to add any of those things if you feel the urge.
There are many useful APIs in the Console class. You may render the grid line by line using Write and WriteLine or you may render it using the SetCursorPosition API. You may redraw the entire grid each time or simply replace the sections that have changed.
You may find it useful to set the size of the console window to something a little larger. There are APIs to do this in the ConsoleYou may find it useful to set the size of the console window to something a little larger. There are APIs to do this in the Console class. The example above is just one style of drawing – feel free to experiment with different characters and grid sizes.
Explanation / Answer
import random gameLength = 8 def print_board(board): for row in board: print " ".join(row) ##Randomly generates integers to place the battleship def random_row(board): return random.randint(1,len(board)) def random_col(board): return random.randint(1,len(board[0])) turn = 0 playAgain = "y" ##While playAgain is equal to y the game will loop and a player ##can start the game over. When they answer n the game will end. while playAgain.lower() == "y": ##Makes a grid filled with O's that is 5 x 5 and prints the game board ##Placed in the while loop to reset board on subesquent games board = [] for x in range(0,5): board.append(["O"] * 5) ################################# ##DEBUGGING Comment Out On PROD## ################################# ##print "Board Length: " + str(len(board)) print "Let's play Battleship!" print_board(board) ##Sets the placement of the battleship ship_row = random_row(board) ship_col = random_col(board) ################################# ##DEBUGGING Comment Out On PROD## ################################# print "Answer Row: " + str(ship_row) print "Answer Column: " + str(ship_col) ##This while loop checks to see how many guesses someone has made. ##Once turn equals game gameLength the game is over while turn 5) or (guess_col < 1 or guess_col > 5): print "Oops, that's not even in the ocean." if turn == gameLength: print "Game Over" break elif(board[guess_row - 1][guess_col - 1] == "X"): print "You guessed that one already." if turn == gameLength: print "Game Over" break else: print "You missed my battleship!" board[guess_row - 1][guess_col - 1] = "X" print "Turn: " + str(turn) if turn == gameLength: print "Game Over" break if (gameLength - turn - 1) > 1: print "You have " + str(gameLength - turn) + " guesses remaining." else: print "You have " + str(gameLength - turn) + " guess remaining." print_board(board) playAgain = raw_input("Would you like to play again? (y/n)") print str("Thanks for playing!")Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.