PDA

View Full Version : C sharp help (NEWBIE)



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();
}
}
}
}

chrisg
11-18-2010, 10:32 AM
Hi silverspawn,

Your first problem is using less than instead of less than or equal to.


for (int ROW = 0; ROW < SEA_HEIGHT_Y; ROW = ROW + 1)
{
for (int COL = 0; COL < SEA_WIDTH_X; COL = COL + 1)


So, 1 through 10 will evaluate as true, but as soon as you get to 11, 11 is not less than 11, so it becomes false. If you want 11 lines, you need to change it to a <= operator, which will evaluate as true when it hits 11, but false when it hits 12.

I'm having trouble reading the logic of your hit detection. IF player says no then that coordinate automatically changes to a hit state?

silverspawn
11-18-2010, 03:03 PM
hey Chrisg

In regards to the grid, I tried doing the <= but everytime it kept crashing...Im I doing something wrong
I never knew programming could be so complex and I thought Maya was hard to use :)


J.T.

silverspawn
11-22-2010, 04:34 PM
hey guys thanks for the response I kinda figured it out, it was the else statement I was using that was making a row dissappear , just on the way to make the a.i player now just got a few tweaks to my vaildations to do as it is only counting the attacks and not misses:)

J.T.

silverspawn
11-24-2010, 07:09 AM
Hey Bravo"zero that would be great...Though gotta admit starting out in programming is a lot harder than what I thought.

I was wondering in my game of battleships I wanted to add some validation to the board in the getplayermove method, I was thinking of a do while/ if loop. so that when you choose your space on the board you are unable to choose outside the array not quite sure how to fit it in have an idea??

static void getPlayerMove()
{
int xCoOrdinate;
int yCoOrdinate;
bool invalidInput = false;
do
{
Console.WriteLine(" Select x co-ordinate");// Selection of the x coordinate
xCoOrdinate = int.Parse(Console.ReadLine());
Console.WriteLine(" Select y co-ordinate"); //Selection of the y coordinate
yCoOrdinate = int.Parse(Console.ReadLine());
{

while(yCoOrdinate < 0 || xCoOrdinate < 0 || yCoOrdinate > 10 || xCoOrdinate > 10);

Console.WriteLine("InvalidInput - Please try again");
invalidInput = true;
}
if (sea[yCoOrdinate, xCoOrdinate] == SeaState.EmptySea)
{
Console.WriteLine("You Missed!");
sea[yCoOrdinate, xCoOrdinate] = SeaState.Miss; // Declares A miss
}
else if (sea[yCoOrdinate, xCoOrdinate] == SeaState.Attack || sea[yCoOrdinate, xCoOrdinate] == SeaState.EmptySea)
{
Console.WriteLine("Shot Wasted"); // Declares A Wasted Shot
}
else
{
Console.WriteLine("HIT!"); // Declares A Hit
sea[yCoOrdinate, xCoOrdinate] = SeaState.Attack;
numberOfHits++;
if (numberOfHits == numberOfShips)
{
GameOver = true;

I got the part were the input is asked for , but i am struggling,just cannot seem to think. Will it be like something on the above code I have put up???

Want to get this working so I can start something in Xna, just need more understanding of computer langyages

Thanks Again J.T.

silverspawn
11-29-2010, 10:19 AM
Thanks for that B2Z I will go through them,

as for battleships managed to get it sorted over the weekend, just gonna go over testing it to make sure it suppose to do what i have tried to make it do :)

And hopefully put a attempted finished version by end of week:)