0% found this document useful (0 votes)
59 views3 pages

Code

This document contains C# code that defines classes for a Battleship game board, player, and program. The BattleshipBoard class contains a method to display the game board. The Player class tracks hit/miss counts and allows getting/setting the game grid and getting random coordinates. The main method initializes the board and player, randomizes ships, runs a loop to get player coordinates until they win, then ends the game by displaying stats.

Uploaded by

Michael Lowry
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views3 pages

Code

This document contains C# code that defines classes for a Battleship game board, player, and program. The BattleshipBoard class contains a method to display the game board. The Player class tracks hit/miss counts and allows getting/setting the game grid and getting random coordinates. The main method initializes the board and player, randomizes ships, runs a loop to get player coordinates until they win, then ends the game by displaying stats.

Uploaded by

Michael Lowry
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

using using using using

System; System.Collections.Generic; System.Linq; System.Text;

namespace BattleShip_Final { class BattleshipBoard { public void DisplayBoard(char[,] Board) { int Row; int Column; Console.WriteLine(" 0 1 2 3 4 5 6 7 8 9"); Console.WriteLine("--+--------------------"); for (Row = 0; Row <= 9; Row++) { Console.Write((Row).ToString() + " "); for (Column = 0; Column <= 9; Column++) { Console.Write(Board[Column, Row] + " "); } Console.WriteLine(); } Console.WriteLine("\n"); } } class Player { char[,] Grid = new char[10, 10]; public int HitCount = 0; public int MissCount = 0; int x = 0; int y = 0; public int getHitCount() { return HitCount; } public int getMissCount() { return MissCount; } public void AskCoordinates() { Console.WriteLine("Enter X"); string line = Console.ReadLine(); int value; if (int.TryParse(line, out value)) { x = value; } else { Console.WriteLine("Not an integer!");

} Console.WriteLine("Enter Y"); line = Console.ReadLine(); if (int.TryParse(line, out value)) { y = value; } else { Console.WriteLine("Not an integer!"); } try { if (Grid[x, y].Equals('S')) { Grid[x, y] = 'H'; Console.Clear(); Console.WriteLine("Hit!\r\n"); HitCount += 1; } else { Grid[x, y] = 'M'; Console.Clear(); Console.WriteLine("Miss!\r\n"); MissCount += 1; } } catch { Console.Clear(); Console.WriteLine("Error: Please enter numbers between 0 and 9. (Inclusive)"); } } public char[,] GetGrid() { return Grid; } public void SetGrid(int q, int w) { Grid[q, w] = 'S'; } public void Randomize() { Random r = new Random(2); //1 of length 2 SetGrid(1, 2); SetGrid(2, 2); //2 of length 3 SetGrid(4, 3); SetGrid(4, 4); SetGrid(4, 5); SetGrid(5, 0); SetGrid(6, 0); SetGrid(7, 0);

//1 of length 4 SetGrid(0, 8); SetGrid(1, 8); SetGrid(2, 8); SetGrid(3, 8); //1 of length 5 SetGrid(7, 4); SetGrid(7, 5); SetGrid(7, 6); SetGrid(7, 7); SetGrid(7, 8); } } class Program { static void Main(string[] args) { Console.Title = "BerdShip!"; Console.WriteLine("Welcome to Berdship!\r\n\r\n"); Console.WriteLine("What is your name?"); string name = System.Console.ReadLine(); Console.WriteLine(); BattleshipBoard b = new BattleshipBoard(); Player p = new Player(); p.Randomize(); while (p.getHitCount() < 17) { b.DisplayBoard(p.GetGrid()); p.AskCoordinates(); } Console.WriteLine("Congratulations, " + name + "! You Win!\r\n"); Console.WriteLine("You missed: " + p.getMissCount() + " times\r\n"); Console.WriteLine("Thanks for playing BerdShip. Press enter to quit. "); System.Console.ReadLine(); } } }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy