silverspawn
11-18-2010, 06:51 AM
Hello just wondering if any of you could help me out as a newbie , I know it is not directly related to xna but I am trying to create a console game battleship in C#.
I have just started to try create from what I have learned from books and Uni, I have been on and off with C# for about a 8 months as maya and 3D apps are my 1st love. But I am trying though:)
But anyway these are the problems I am having?
1. for some reason my grid is missing a row/col which is why some of my ships are not being displayed not sure what to do as I have tried but...knowing me it is most probably something really simple??
2.I am also having problems validating a direct hit of a ship,not quite sure what I need to do next....most my code makes sense have a look to see what you think :)
Any help would be grateful, I just have a real problem gluing it altogether:)
J.T.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jasons_BattleShip_game1 //Jason,s attempt at a Battleship game :).
{
class Program
{
enum SeaState // decides on the various types of the board state.
{
Battleship,
Cruiser,
Submarine,
RowingBoat,
EmptySea,
Attack
};
const int SEA_WIDTH_X = 11;// Sets board height and width to the required size
const int SEA_HEIGHT_Y = 11;
static int numberOfHits = 0;
static bool GameOver = false;
static SeaState[,] sea = new SeaState[SEA_HEIGHT_Y, SEA_WIDTH_X];
static void ClearSea()//Sets all spaces on board to hidden.
{
for (int ROW = 0; ROW < SEA_HEIGHT_Y; ROW = ROW + 1)
{
for (int COL = 0; COL < SEA_WIDTH_X; COL = COL + 1)
{
sea[ROW, COL] = SeaState.EmptySea;
}
}
}
static void PlaceShips() //Set co-ordinates for Ships.
{
sea[0, 5] = SeaState.Battleship;
sea[2, 7] = SeaState.Battleship;
sea[0, 0] = SeaState.Cruiser;
sea[2, 0] = SeaState.Cruiser;
sea[4, 3] = SeaState.Cruiser;
sea[5, 6] = SeaState.Submarine;
sea[8, 8] = SeaState.RowingBoat;
}
static void displaySea() //This will display the board,and will also change the colour of the board being displayed.
{
{
Console.WriteLine("<<<<<<Welcome To BattleShips>>>>>>");// Game tiltle
Console.WriteLine("\n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Why Not Expand Your Console Window?");
Console.WriteLine("\n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("0 : Empty Space");
Console.WriteLine("1 : Attack");
Console.WriteLine("2 : BattleShip");
Console.WriteLine("3 : Cruiser");
Console.WriteLine("4 : Submarine");
Console.WriteLine("5 : RowingBoat");
Console.WriteLine("\n");
for (int COL = 0; COL < SEA_WIDTH_X; COL = COL + 1)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0,2}", COL);
Console.ForegroundColor = ConsoleColor.White;
}
}
Console.WriteLine("\n");
for (int ROW = 0; ROW < SEA_HEIGHT_Y; ROW = ROW + 1)
{
for (int COL = 0; COL < SEA_WIDTH_X; COL = COL + 1)
{
if (COL == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0,2}", ROW);
Console.ForegroundColor = ConsoleColor.White;
}
else
{
SeaState seastate = sea[ROW, COL];// This will display the board evenly
switch (seastate)
{
case SeaState.EmptySea:
Console.ForegroundColor = ConsoleColor.White;
Console.Write("{0,2}", "0");
break;
case SeaState.Attack:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "1");
break;
case SeaState.Battleship:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "2");
break;
case SeaState.Cruiser:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "3");
break;
case SeaState.Submarine:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "4");
break;
case SeaState.RowingBoat:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "5");
break;
default:
break;
}
}
}
Console.WriteLine("\n");
}
}
static void getPlayerMove()// This will let the player decide were he wants to move using the x,y co-ordinate and then let them decide if there is a Enemy Ship there.
{
bool playerMove = true;
Console.WriteLine(" Select x co-ordinate");
int xCoOrdinate = int.Parse(Console.ReadLine());
Console.WriteLine(" Select y co-ordinate");
int yCoOrdinate = int.Parse(Console.ReadLine());
Console.WriteLine("Direct hit, You get another turn");
string playerDecision = Console.ReadLine();
if (playerDecision.ToUpper() == "Y")
{
playerMove = true;
}
else if (playerDecision.ToUpper() == "N")
{
playerMove = false;
}
else
{
Console.WriteLine("Invalid choice - Attack is off grid or Wrong input");
getPlayerMove();
if (playerMove == false && sea[yCoOrdinate, xCoOrdinate] == SeaState.Attack && sea[yCoOrdinate, xCoOrdinate] != SeaState.Attack)
{
sea[yCoOrdinate, xCoOrdinate] = SeaState.Attack; //If ship is there then game changes to Attacked ship, then adds one to the number of attacked ships
numberOfHits++;
}
else if (playerMove == true && sea[yCoOrdinate, xCoOrdinate] == SeaState.Attack)
{
GameOver = false;//Game over if ship there, but they think there is not
}
else if (playerMove == false && sea[yCoOrdinate, xCoOrdinate] == SeaState.EmptySea)
{
sea[yCoOrdinate, xCoOrdinate] = SeaState.EmptySea;//Lets the player continue
}
}
}
static void Main()
{
numberOfHits = 0;//sets the number of ships that have been attacked to 0
GameOver = false;//game continue until game is true
ClearSea();//call the clearSea method to set all spaces to Empty
PlaceShips();//places Ships on board
do
{
displaySea();//display board
getPlayerMove();//takes x,y co-ordinates from player to move
}
while (numberOfHits < 7 && GameOver == false);
if (numberOfHits == 7)
{
Console.WriteLine("Congratulations you win");
}
else
{
Console.WriteLine("You have lost the game");
}
Console.WriteLine("press y if you would like to play again \n or any other key to exit");
string playAgain = Console.ReadLine();
if (playAgain.ToUpper() == "Y")
{
Main();
}
}
}
}
I have just started to try create from what I have learned from books and Uni, I have been on and off with C# for about a 8 months as maya and 3D apps are my 1st love. But I am trying though:)
But anyway these are the problems I am having?
1. for some reason my grid is missing a row/col which is why some of my ships are not being displayed not sure what to do as I have tried but...knowing me it is most probably something really simple??
2.I am also having problems validating a direct hit of a ship,not quite sure what I need to do next....most my code makes sense have a look to see what you think :)
Any help would be grateful, I just have a real problem gluing it altogether:)
J.T.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Jasons_BattleShip_game1 //Jason,s attempt at a Battleship game :).
{
class Program
{
enum SeaState // decides on the various types of the board state.
{
Battleship,
Cruiser,
Submarine,
RowingBoat,
EmptySea,
Attack
};
const int SEA_WIDTH_X = 11;// Sets board height and width to the required size
const int SEA_HEIGHT_Y = 11;
static int numberOfHits = 0;
static bool GameOver = false;
static SeaState[,] sea = new SeaState[SEA_HEIGHT_Y, SEA_WIDTH_X];
static void ClearSea()//Sets all spaces on board to hidden.
{
for (int ROW = 0; ROW < SEA_HEIGHT_Y; ROW = ROW + 1)
{
for (int COL = 0; COL < SEA_WIDTH_X; COL = COL + 1)
{
sea[ROW, COL] = SeaState.EmptySea;
}
}
}
static void PlaceShips() //Set co-ordinates for Ships.
{
sea[0, 5] = SeaState.Battleship;
sea[2, 7] = SeaState.Battleship;
sea[0, 0] = SeaState.Cruiser;
sea[2, 0] = SeaState.Cruiser;
sea[4, 3] = SeaState.Cruiser;
sea[5, 6] = SeaState.Submarine;
sea[8, 8] = SeaState.RowingBoat;
}
static void displaySea() //This will display the board,and will also change the colour of the board being displayed.
{
{
Console.WriteLine("<<<<<<Welcome To BattleShips>>>>>>");// Game tiltle
Console.WriteLine("\n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Why Not Expand Your Console Window?");
Console.WriteLine("\n");
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("0 : Empty Space");
Console.WriteLine("1 : Attack");
Console.WriteLine("2 : BattleShip");
Console.WriteLine("3 : Cruiser");
Console.WriteLine("4 : Submarine");
Console.WriteLine("5 : RowingBoat");
Console.WriteLine("\n");
for (int COL = 0; COL < SEA_WIDTH_X; COL = COL + 1)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0,2}", COL);
Console.ForegroundColor = ConsoleColor.White;
}
}
Console.WriteLine("\n");
for (int ROW = 0; ROW < SEA_HEIGHT_Y; ROW = ROW + 1)
{
for (int COL = 0; COL < SEA_WIDTH_X; COL = COL + 1)
{
if (COL == 0)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("{0,2}", ROW);
Console.ForegroundColor = ConsoleColor.White;
}
else
{
SeaState seastate = sea[ROW, COL];// This will display the board evenly
switch (seastate)
{
case SeaState.EmptySea:
Console.ForegroundColor = ConsoleColor.White;
Console.Write("{0,2}", "0");
break;
case SeaState.Attack:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "1");
break;
case SeaState.Battleship:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "2");
break;
case SeaState.Cruiser:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "3");
break;
case SeaState.Submarine:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "4");
break;
case SeaState.RowingBoat:
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("{0,2}", "5");
break;
default:
break;
}
}
}
Console.WriteLine("\n");
}
}
static void getPlayerMove()// This will let the player decide were he wants to move using the x,y co-ordinate and then let them decide if there is a Enemy Ship there.
{
bool playerMove = true;
Console.WriteLine(" Select x co-ordinate");
int xCoOrdinate = int.Parse(Console.ReadLine());
Console.WriteLine(" Select y co-ordinate");
int yCoOrdinate = int.Parse(Console.ReadLine());
Console.WriteLine("Direct hit, You get another turn");
string playerDecision = Console.ReadLine();
if (playerDecision.ToUpper() == "Y")
{
playerMove = true;
}
else if (playerDecision.ToUpper() == "N")
{
playerMove = false;
}
else
{
Console.WriteLine("Invalid choice - Attack is off grid or Wrong input");
getPlayerMove();
if (playerMove == false && sea[yCoOrdinate, xCoOrdinate] == SeaState.Attack && sea[yCoOrdinate, xCoOrdinate] != SeaState.Attack)
{
sea[yCoOrdinate, xCoOrdinate] = SeaState.Attack; //If ship is there then game changes to Attacked ship, then adds one to the number of attacked ships
numberOfHits++;
}
else if (playerMove == true && sea[yCoOrdinate, xCoOrdinate] == SeaState.Attack)
{
GameOver = false;//Game over if ship there, but they think there is not
}
else if (playerMove == false && sea[yCoOrdinate, xCoOrdinate] == SeaState.EmptySea)
{
sea[yCoOrdinate, xCoOrdinate] = SeaState.EmptySea;//Lets the player continue
}
}
}
static void Main()
{
numberOfHits = 0;//sets the number of ships that have been attacked to 0
GameOver = false;//game continue until game is true
ClearSea();//call the clearSea method to set all spaces to Empty
PlaceShips();//places Ships on board
do
{
displaySea();//display board
getPlayerMove();//takes x,y co-ordinates from player to move
}
while (numberOfHits < 7 && GameOver == false);
if (numberOfHits == 7)
{
Console.WriteLine("Congratulations you win");
}
else
{
Console.WriteLine("You have lost the game");
}
Console.WriteLine("press y if you would like to play again \n or any other key to exit");
string playAgain = Console.ReadLine();
if (playAgain.ToUpper() == "Y")
{
Main();
}
}
}
}