Re: Battleship Application Assignment Help

  • From: "black ares" <matematicianu2003@xxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Tue, 25 Aug 2009 14:57:25 +0300

First of all, I don't know how you passed your other assignments.
Your code shows that you don't learned very well the c language and over all you don't know even programming.
If you try to make a career in this, forget it.

Second, your faults:
1. How I remember, every declaration of function needs to have its parameter specified with their types.
So the check function has (int xpos,ypos,player) is wrong.
The compiler must issue a compile error for this.
I guess you wanted to write (int xpos,int ypos,int player), but it seems that you don't know even to read a error message.

2. in the main function you do:
checkPosition(int xpos, ypos, player);
this is a call to a function where you have not to specify the type of parameters, but you must declare that variables you use there.
In the main function I see not xpos, ypos, player variables declared.
So the compiler must complain again with some errors, but you don't read them.

3. How I remember, in c/c++ the equality testing operator is == not =.
= operator is for assigning values to variables, but you do:
if (p1board[xpos][ypos] = '*') & (p1board[xpos][ypos] = 's')
This line of code modify the position of the array first with * second with s qafter it apply the & operator which is bitwise and, will give s returned in asccii code will give something greather than 0 so in c++ this will mean true. So that line of code will always return true or will have a unpredictable behavior at run time.

4. the logical and operator is && not & which is bitwise and operator.
they have different type of parameters, and return different values.

You must restart study your material from the begining.
To understand at first, what is programming and what are the key concepts of programming. What is a variable, a constant, a function, a return type, a parameter, pass by reference, pass by value, an operator, a statement.
What are the programming paradigms and what concepts do them use.
If you think that if you learn a programming language, you know how to program you are wrong.








----- Original Message ----- From: "Marvin Hunkin" <startrekcafe@xxxxxxxxx>
To: <programmingblind@xxxxxxxxxxxxx>
Sent: Tuesday, August 25, 2009 1:37 PM
Subject: Battleship Application Assignment Help


hi.
first off.
tried searching on google for this today.
a simple battleship game.
well, did not find any thing.
found heaps of examples.
but either too complex for my requiremenetsrequirements , or for java, or c#
or visual basic.
had a couple of gos at this.
so would like your input.
see if i am going about it the right way or the wrong way.
or maybe you can point me to a simple battleships game, modified.
so will paste the session notes and my code below.
if any one can help me out.
got all my other assignments working.
this one is a bit of a challenge.
trying to modify it from the sample program, which was a norts and crosses
game.
but my leturer, is so over worked, and teaching face to face, at multiple
locations, and also has other online students as well, and also on
comittees, organising meetings, for ccurriculum, etc.
and got a fmaily and a life.
so i am the only blind programmer.
and could be waiting weeks for him to reply.
not even sure if his computer system is up and running.
did get one e-mail from him yesterday.
so that is the first communication had from him in a few weeks.
so now, if any one can help, will paste my code below and the session notes.
all about multi dimentional arrays.
cheers Marvin.

code starts here:

/*
Program: BattleShip
File:  battleship.cpp
Description: BattleShips program.
Author:  Marvin Hunkin.
Revision: 1.00 25/08/2009 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>
using namespace std;

void initializeBoard();
void displayBoard();
void setupShips();
bool checkPosition(int xpos, ypos, player);
void playGame();

char p1board[10][10];
char p2board[10][10];

int main()
{
initializeBoard();
displayBoard();
setupShips();
checkPosition(int xpos, ypos, player);
playGame();
return 0;
} // close main function
// Initialize Board function.
void initializeBoard()
{
for (int i = 0; i<10; i++)
{
 for (int j = 0; j < 10; j++)
 {
  p1board[i][j] = 's';
  p2board[i][j] = 's';
 } // close inner for loop.
} // close outer for loop.
} // close initializeBoard function.

// Display Board function.
void displayBoard()
{
cout << "\n1 2 3 4 5 6 7 8 9\n";
for (int i = 0; i < 10; i++)
{
 cout << i + 1;
 for (int j = 0; j < 10; j++)
 {
 cout << p1board[i][j];
 cout << p2board[i][j];
} // close inner for loop.
cout << endl;
} // close outer for loop.
cout << "\n\n";
} // close displayBoard function.

// Setup Ships function goes here.
void setupShips()
{
for (int i = 0; i < 6; i++)
{
 for (int j = 0; j < 6; j++)
 {
  p1board[i][j] = '*';
  p2board[i][j] = '*';
 } // close inner for loop.
} // close outer for loop.
} // close setupShips function.

// bool CheckPosition goes here.
bool checkPosition(int xpos, ypos)
{

if (p1board[xpos][ypos] = '*') & (p1board[xpos][ypos] = 's')
{
return true;
} // close if block.
return false;
else if (p2board[xpos][ypos] = '*') & (p2board[xpos][ypos] = 's')
{
return true;
} // close else if block.
return false;
} // close checkPosition function.

// playGame function goes here.
// Declare variables.
char miss = 'm';
char hit = 'h';
char mark = '*';
int xpos, ypos, player;
bool validMove;
char winner = '?';

for (int i =1; i < 10; i++)
{
cout << "Player 1" << player<< "enter your row value:";
cin>> xpos;
cout << "player 1" << player << "enter your colum value:";
cin>> ypos;
xpos -=1;
ypos -= 1;
validMove = checkPosition (xpos, ypos);
while (validMove)
{
    cout << "That is an invalid move\n";
cout << "Player 1" << player<< "enter your row value:";
cin>> xpos;
cout << "player 1" << player << "enter your colum value:";
cin>> ypos;
   xpos -=1;
   ypos -=1;
  validMove = checkPosition(xpos, ypos);
 } // close while loop.
p1board[xpos][ypos] = mark;
p2board[xpos][ypos] = mark;
if (mark == 'X')
{
switch(mark)
{
 case 1:
 miss = 'm';
 cout << " You missed!";
 break;
 case 2:
 hit = 'h';
 cout << "Direct hit!";
 break;
} // close switch block.
} // close if block.
displayBoard();
if (player 1 = true)
{
cout << "Player 1 is the winner" << player << endl;
} // close if block.
else if (player 2 = true)
{
cout << " Player 2 is the winner" << player << endl;
} // close else if block.
} // close playGame function.

Session Web Page Starts Here:
--------------------------------------------------------------------------------

Session 13
Multi Dimension Arrays

--------------------------------------------------------------------------------

Objectives
The purpose of this session is to introduce the multi dimension array data
structure. The multi demension array can be thought of as a more complex
form of array which essentially takes the form of a grid.After completing
this exercise, you should have a firm grasp of multi dimension arrays, as
well as reinforcing your understanding of arrays generally.



--------------------------------------------------------------------------------

To extend on what we did in the last session, this session we will create an
array of arrays. These are called multi dimensional arrays. Multi
dimensional arrays can be very powerful tools for the programer. They can
also be a little difficult to understand. The most common type of
multidimensional array that you will encounter are two dimensional arrays.
The best way to think of these is as tables, were you have rows and columns.
You refer to one cell by giving its row and column reference.

If you think about the children's game of naughts and crosses, we have a 3
by 3 grid. We can use a two dimensional array to model the board. We can
also use the array indexes to place a O or a X into the array where the
player wishes to put the mark. This program will require a number of
functions to make the process easier to create. We will start with by
including iostream and a main function.


/*
* Program: Multi Dimension Arrays example - 1
* File:  m_arrays1.cpp
* Description: This program is an exercise to demonstrate the use of multi
dimension arrays in a program.
* Author:  Henry Bush
* Revision: 1.00 2/12/2003 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>using namespace std;

int main()
{
} //close main function


We will declare an array that will hold char values. The values will be
either a O or an X for the players move and a * for a cell that has not had
a mark in it yet. So add the following char array declaration before the
main function


char board[3][3];


The next thing that we are going to do is to create a function to display a
title and some instruction to the users. This function will have no return
value and take no parameters. Place the declaration before the array
declaration and the implementation after the main function


void title();

void title()
{
cout << "\t\tWelcome to O's and X's\nA game for two players\n\n";
cout << "To play this game you enter to numbers, less than 4\n ";
cout << "referring to the row and the column that you wish to\n ";
cout << "place you mark\n\n";
} // close title function


The next thing that we need to do is to initialise the board. We will also
place this into another function. This function will also take no parameters
and return no values


void initialiseBoard();

void initialiseBoard()
{
for(int i= 0; i < 3; i++)
{
 for(int j= 0; j < 3; j++)
 {
  board[i][j] = '*';
 } // close inner for loop
}  // close outer for loop
}   // close initialiseBoard function


The next thing that we are going to do is to display the board. Again this
function will have no parameters or return values. It will simply display
the board whenever we need to show it. This will be after every move.


void displayBoard();

void displayBoard()
{
cout << "\n 123" << endl;
for(int i= 0; i < 3; i++)
{
 cout << (i +1);
 for(int j= 0; j < 3; j++)
 {
  cout << board[i][j];
 } // close inner for loop
 cout << endl;
}  // close outer for loop
cout << "\n\n";
}   // close displayBoard function


So far everything has been fairly straight forward. The next function that
we are going to write will check the values that the user enters and see if
that position has already had a mark put into it. If the position has not
had a value placed into it then the function will return a true value. If
the position has already had a mark then the function will return false. The function will be passed two int values that are the array indexes that refer
to the position that the user wishes to place a mark


bool checkPosition(int x ,int y);

bool checkPosition(int x, int y)
{
if(board[x][y] != '*')
{
 return true;
} // close if block
return false;
}  // close checkPosition function


We are getting close to having all of the functions that we need for this
program. We need to check if one of the users has won the game. To do this
we will create a function that returns a char. This char value will contain the name of character that represents the winner(X or O) or a question mark, indicating that there is no winner at this check. To determine if there is a winner we need to look at each row column and diagonal and see if the values have the same non * value. This function ends up being large, simply by the
nature of what it is doing.


char checkWinner();

char checkWinner()
{
char checkValue = '?';
if((board[0][0] == board[0][1]) && (board[0][0] == board[0][2]) &&
(!(board[0][0] == '*')))
{
 checkValue = board[0][0];
} // close if block

else if((board[1][0] == board[1][1]) && (board[1][0] == board[1][2]) &&
(!(board[1][0] == '*')))
{
 checkValue = board[1][0];
} // close else if block

else if((board[2][0] == board[2][1]) && (board[2][0] == board[2][2]) &&
(!(board[2][0] == '*')))
{
 checkValue = board[2][0];
} // close else if block

else if((board[0][0] == board[1][0]) && (board[0][0] == board[2][0]) &&
(!(board[0][0] == '*')))
{
 checkValue = board[0][0];
} // close else if block

else if((board[0][1] == board[1][1]) && (board[0][1] == board[1][2]) &&
(!(board[0][1] == '*')))
{
 checkValue = board[0][1];
} // close else if block

else if((board[0][2] == board[1][2]) && (board[0][2] == board[2][2]) &&
(!(board[0][2] == '*')))
{
 checkValue = board[0][2];
} // close else if block

else if((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) &&
(!(board[0][0] == '*')))
{
 checkValue = board[0][0];
} // close else if block

else if((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) &&
(!(board[0][2] == '*')))
{
 checkValue = board[0][2];
} // close else if block

else
{
 checkValue = '?';
} // close else block

return checkValue;
}  // close checkWinner function


This has all of the utility functions for this program. The next thing that
we need to do is to create a function that control the way the game is
played. For this simple program we will implement a for loop that will
iterate nine times. The game must end at that time as there is no more moves
that can be made. Inside the loop we need to tell the player whose turn it
is to enter the coordinates for the position that they wish to place their
mark. When the player has done that then we need to check to see if it is a
valid move. If they have entered an invalid move then we will hold the
program in a while loop until they enter a valid move. Next, we place the
mark, swap players and then check if there has been a winner with that move. If there has been a winner then inform the players and exit the program . If
there has been no winner in that turn then allow the for loop to iterate
again.


void playGame();

void playGame()
{
char mark ='X';
int xpos, ypos;
bool validMove;
char winner = '?';
for(int i = 1; i <= 9; i++)
{
 cout << "Player " << mark << " enter your move row value: ";
 cin >> xpos;
 cout << "Enter the column value: ";
 cin >> ypos;
 xpos -=1;
 ypos -=1;
 validMove = checkPosition(xpos, ypos);
 while(validMove)
 {
  cout << "That is an invalid move\n";
  cout << "Player " << mark << " enter your move row value: ";
  cin >> xpos;
  cout << "Enter the column value: ";
  cin >> ypos;
  xpos -=1;
  ypos -=1;
  validMove = checkPosition(xpos, ypos);
 } // close while loop
 board[xpos][ypos] = mark;
 if(mark == 'X')
 {
  mark = 'O';
 } // close if block
 else
 {
  mark = 'X';
 } // close else block
 displayBoard();
 winner = checkWinner();
 if((winner == 'X')||(winner == 'O'))
 {
  cout << "The Winner is player " << winner << endl;
  exit(0);
 } // close if block
}  // close for loop
cout << "The game was a draw\n " << winner << endl;
}   // close playGame function


The last thing left to do is to add code to the main function. This will
just set things in motion, initialise the board, display the title and the
board and then play the game.


int main()
{

initialiseBoard();
title();
displayBoard();
playGame();
return 0;
} //close main function


Well that's it. There has been a lot of programming in this program, but I
think that bigger programs help in learning how to program. The whole
program follows. Make sure that you understand what is happening in each of
the functions. The exercise for this session will involve you doing
something similar.


/*
* Program: Multi Dimension Arrays example - 1
* File:  m_arrays1.cpp
* Description: This program is an exercise to demonstrate the use of multi
dimension arrays in a program.
* Author:  Henry Bush
* Revision: 1.00 2/12/2003 First release.
*/

// iostream is needed for the cout statement.
#include <iostream>using namespace std;

void title();
void initialiseBoard();
void displayBoard();
bool checkPosition(int x ,int y);
void playGame();
char checkWinner();

char board[3][3];

int main()
{

initialiseBoard();
title();
displayBoard();
playGame();
return 0;
} //close main function

void title()
{
cout << "\t\tWelcome to O's and X's\nA game for two players\n\n";
cout << "To play this game you enter to numbers, less than 4\n ";
cout << "refering to the row and the column that you wish to\n ";
cout << "place you mark\n\n";
} // close title function

void initialiseBoard()
{
for(int i= 0; i < 3; i++)
{
 for(int j= 0; j < 3; j++)
 {
  board[i][j] = '*';
 } // close inner for loop
}  // close outer for loop
}   // close initialiseBoard function

void displayBoard()
{
cout << "\n 123" << endl;
for(int i= 0; i < 3; i++)
{
 cout << (i +1);
 for(int j= 0; j < 3; j++)
 {
  cout << board[i][j];
 } // close inner for loop
 cout << endl;
}  // close outer for loop
cout << "\n\n";
}   // close displayBoard function

void playGame()
{
char mark ='X';
int xpos, ypos;
bool validMove;
char winner = '?';
for(int i = 1; i <= 9; i++)
{
 cout << "Player " << mark << " enter your move row value: ";
 cin >> xpos;
 cout << "Enter the column value: ";
 cin >> ypos;
 xpos -=1;
 ypos -=1;
 validMove = checkPosition(xpos, ypos);
 while(validMove)
 {
  cout << "That is an invalid move\n";
  cout << "Player " << mark << " enter your move row value: ";
  cin >> xpos;
  cout << "Enter the column value: ";
  cin >> ypos;
  xpos -=1;
  ypos -=1;
  validMove = checkPosition(xpos, ypos);
 } // close while loop
 board[xpos][ypos] = mark;
 if(mark == 'X')
 {
  mark = 'O';
 } // close if block
 else
 {
  mark = 'X';
 } // close else block
 displayBoard();
 winner = checkWinner();
 if((winner == 'X')||(winner == 'O'))
 {
  cout << "The Winner is player " << winner << endl;
  exit(0);
 } // close if block
}  // close for loop
cout << "The game was a draw\n " << winner << endl;
}   // close playGame function

bool checkPosition(int x, int y)
{
if(board[x][y] != '*')
{
 return true;
} // close if block
return false;
}  // close checkPosition function

char checkWinner()
{
char checkValue = '?';
if((board[0][0] == board[0][1]) && (board[0][0] == board[0][2]) &&
(!(board[0][0] == '*')))
{
 checkValue = board[0][0];
} // close if block

else if((board[1][0] == board[1][1]) && (board[1][0] == board[1][2]) &&
(!(board[1][0] == '*')))
{
 checkValue = board[1][0];
} // close else if block

else if((board[2][0] == board[2][1]) && (board[2][0] == board[2][2]) &&
(!(board[2][0] == '*')))
{
 checkValue = board[2][0];
} // close else if block

else if((board[0][0] == board[1][0]) && (board[0][0] == board[2][0]) &&
(!(board[0][0] == '*')))
{
 checkValue = board[0][0];
} // close else if block

else if((board[0][1] == board[1][1]) && (board[0][1] == board[1][2]) &&
(!(board[0][1] == '*')))
{
 checkValue = board[0][1];
} // close else if block

else if((board[0][2] == board[1][2]) && (board[0][2] == board[2][2]) &&
(!(board[0][2] == '*')))
{
 checkValue = board[0][2];
} // close else if block

else if((board[0][0] == board[1][1]) && (board[0][0] == board[2][2]) &&
(!(board[0][0] == '*')))
{
 checkValue = board[0][0];
} // close else if block

else if((board[0][2] == board[1][1]) && (board[0][2] == board[2][0]) &&
(!(board[0][2] == '*')))
{
 checkValue = board[0][2];
} // close else if block

else
{
 checkValue = '?';
} // close else block

return checkValue;
}  // close checkWinner function



--------------------------------------------------------------------------------
Session 13 Practical Exercise
For this exercise you will create a game yourself. The game will be a
modified version of battleships. If you are not familiar with the game then
a quick overview will help you to create the program for this exercise.

The game starts with you placing a number of ships onto a grid and your
opponent does the same. Then taking turns each player calls out a grid
coordinate. If that matches where the opponent has placed one of his ships
then it is sunk. Either way you place a marker on a grid to indicate where
you have sent a missile. The game continues until one player has had his
whole fleet sunk.

For this program the board will be a ten by ten grid and you will place six
ships in this grid.

If you can complete this exercise without any difficulties then you can
proceed to the next lesson. If you found that you had any problems with this
exercise then contact your instructor about the problems and attach any
working code so that we can discuss where your solutions lay.

After you successfully complete this exercise, remember to send your
completed code into your instructor for assessment.

--------------------------------------------------------------------------------

ps: i would like an answer if possible tonight or tomorrow.
been working on this for the past couple of days, apart from doing other
necessary tasks and other projects.
E-Mail: startrekcafe@xxxxxxxxx
Msn: startrekcafe@xxxxxxx
Skype: startrekcafe
to subscribe to the Jaws Australia group send a blank message to
JawsOz-subscribe@xxxxxxxxxxxxxxx
Visit my Jaws Australia Group at http://groups.yahoo.com/groups/JawsOz/


__________
View the list's information and change your settings at
//www.freelists.org/list/programmingblind


__________
View the list's information and change your settings at //www.freelists.org/list/programmingblind

Other related posts: