What's new

C++;Help needed

Ray_of_Hope

FULL MEMBER
Joined
Jul 5, 2014
Messages
1,377
Reaction score
0
Country
Pakistan
Location
Pakistan
Hey there.
I have to submit an assignmnent today of an sos game in c++,i have made a code but i donot know how to add a function to count score of each player.any help will be greatly appreciated.
@Zibago and other programmers
here is the code;
#include <iostream>
using namespace std;
int currentPlayer = 0; // 0-> player 1. 1->player 2
int score[2]; // indexed by currentPlayer
char board[8][8] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
};


void gameboard();
void getmove(int &row, int &col);
void gather_all();
void win1();

void
gameboard()
{
system("cls");
cout << "player1: " << score[0] << "\t" << "player2: " << score[1] << endl;
cout << "+===+===+===+===+===+===+===+===+" << endl;

for (int i = 0; i < 8; i++) {
cout << "! ";
for (int j = 0; j < 8; j++)
cout << board[j] << " ! ";
cout << endl << "+===+===+===+===+===+===+===+===+" << endl;
}
}


// Get a move from the current player. Set the value in the board and return the row/column
// that they set.
void
getmove(int &row, int &col)
{
char letter;

while (true) {
if (currentPlayer == 0) {
cout << "Player 1 enter the row and column (NUMBERS ONLY!): ";
} else {
cout << "Player 2 enter the row and column (NUMBERS ONLY!): ";
}

if (!(cin >> row >> col)) {
cout << "I SAID NUMBERS ONLY!!!!\n";
cin.clear();
cin.ignore(1000, '\n');
continue; // go back to the start of the loop
}
row--; // immediately convert from 1-8 to 0-7
col--;

// Is the number valid?
if (row < 0 || col < 0 || row > 7 || col > 7) {
cout << "Row and column numbers must be 1-8\n";
continue;
}

if (board[row][col] != ' ') {
cout << "That square is already taken\n";
continue;
}

break; // success
}


// Get the value for the square
while (true) {
cout << "Enter S or O(USE CAPITALS ONLY!): ";
cin >> letter;
if (letter == 'S' || letter == 'O') {
break;
} else {
cout << "I SAID CAPITALS ONLY!!!!. ALSO USE S OR O.";
system("pause");
system("cls");
}
}

board[row][col] = letter;
}


void
win1()
{
}


int
main()
{
int row, col;
while (1) {
gameboard();
getmove(row, col);
currentPlayer = !currentPlayer; // switch players

}
return 0;
}
 
.
I remember back when I used to code on Python. Got pretty good at it eventually.
 
. . . .
a sign integers for each player and record score to that integer and use integer to display that score. I don't know C++ but the concept is same to achieve the task you require.
I have been trying for the last 2 hours.......
 
.
PHP:
#include <iostream>
using namespace std;
int currentPlayer = 0; // 0-> player 1. 1->player 2
int score[2]; // indexed by currentPlayer
char board[8][8] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
};


void gameboard();
void getmove(int &row, int &col);
void gather_all();
void win1();

void
gameboard()
{
system("cls");
cout << "player1: " << score[0] << "\t" << "player2: " << score[1] << endl;
cout << "+===+===+===+===+===+===+===+===+" << endl;

for (int i = 0; i < 8; i++) {
cout << "! ";
for (int j = 0; j < 8; j++)
cout << board[j] << " ! ";
cout << endl << "+===+===+===+===+===+===+===+===+" << endl;
}
}


// Get a move from the current player. Set the value in the board and return the row/column
// that they set.
void
getmove(int &row, int &col)
{
char letter;

while (true) {
if (currentPlayer == 0) {
cout << "Player 1 enter the row and column (NUMBERS ONLY!): ";
} else {
cout << "Player 2 enter the row and column (NUMBERS ONLY!): ";
}

if (!(cin >> row >> col)) {
cout << "I SAID NUMBERS ONLY!!!!\n";
cin.clear();
cin.ignore(1000, '\n');
continue; // go back to the start of the loop
}
row--; // immediately convert from 1-8 to 0-7
col--;

// Is the number valid?
if (row < 0 || col < 0 || row > 7 || col > 7) {
cout << "Row and column numbers must be 1-8\n";
continue;
}

if (board[row][col] != ' ') {
cout << "That square is already taken\n";
continue;
}

break; // success
}


// Get the value for the square
while (true) {
cout << "Enter S or O(USE CAPITALS ONLY!): ";
cin >> letter;
if (letter == 'S' || letter == 'O') {
break;
} else {
cout << "I SAID CAPITALS ONLY!!!!. ALSO USE S OR O.";
system("pause");
system("cls");
}
}

board[row][col] = letter;
}


void
win1()
{
}


int
main()
{
int row, col;
while (1) {
gameboard();
getmove(row, col);
currentPlayer = !currentPlayer; // switch players

}
return 0;
}


Now it's more readable. Do you want to calculate in the main function or create a separate function to calculate the score?
 
.
If I knew pdf helped with assignments I would've asked too ......:(
 
.
PHP:
#include <iostream>
using namespace std;
int currentPlayer = 0; // 0-> player 1. 1->player 2
int score[2]; // indexed by currentPlayer
char board[8][8] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
};


void gameboard();
void getmove(int &row, int &col);
void gather_all();
void win1();

void
gameboard()
{
system("cls");
cout << "player1: " << score[0] << "\t" << "player2: " << score[1] << endl;
cout << "+===+===+===+===+===+===+===+===+" << endl;

for (int i = 0; i < 8; i++) {
cout << "! ";
for (int j = 0; j < 8; j++)
cout << board[j] << " ! ";
cout << endl << "+===+===+===+===+===+===+===+===+" << endl;
}
}


// Get a move from the current player. Set the value in the board and return the row/column
// that they set.
void
getmove(int &row, int &col)
{
char letter;

while (true) {
if (currentPlayer == 0) {
cout << "Player 1 enter the row and column (NUMBERS ONLY!): ";
} else {
cout << "Player 2 enter the row and column (NUMBERS ONLY!): ";
}

if (!(cin >> row >> col)) {
cout << "I SAID NUMBERS ONLY!!!!\n";
cin.clear();
cin.ignore(1000, '\n');
continue; // go back to the start of the loop
}
row--; // immediately convert from 1-8 to 0-7
col--;

// Is the number valid?
if (row < 0 || col < 0 || row > 7 || col > 7) {
cout << "Row and column numbers must be 1-8\n";
continue;
}

if (board[row][col] != ' ') {
cout << "That square is already taken\n";
continue;
}

break; // success
}


// Get the value for the square
while (true) {
cout << "Enter S or O(USE CAPITALS ONLY!): ";
cin >> letter;
if (letter == 'S' || letter == 'O') {
break;
} else {
cout << "I SAID CAPITALS ONLY!!!!. ALSO USE S OR O.";
system("pause");
system("cls");
}
}

board[row][col] = letter;
}


void
win1()
{
}


int
main()
{
int row, col;
while (1) {
gameboard();
getmove(row, col);
currentPlayer = !currentPlayer; // switch players

}
return 0;
}


Now it's more readable. Do you want to calculate in the main function or create a separate function to calculate the score?


System.out.println("Java is the best");
 
.
PHP:
#include <iostream>
using namespace std;
int currentPlayer = 0; // 0-> player 1. 1->player 2
int score[2]; // indexed by currentPlayer
char board[8][8] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '
};


void gameboard();
void getmove(int &row, int &col);
void gather_all();
void win1();

void
gameboard()
{
system("cls");
cout << "player1: " << score[0] << "\t" << "player2: " << score[1] << endl;
cout << "+===+===+===+===+===+===+===+===+" << endl;

for (int i = 0; i < 8; i++) {
cout << "! ";
for (int j = 0; j < 8; j++)
cout << board[j] << " ! ";
cout << endl << "+===+===+===+===+===+===+===+===+" << endl;
}
}


// Get a move from the current player. Set the value in the board and return the row/column
// that they set.
void
getmove(int &row, int &col)
{
char letter;

while (true) {
if (currentPlayer == 0) {
cout << "Player 1 enter the row and column (NUMBERS ONLY!): ";
} else {
cout << "Player 2 enter the row and column (NUMBERS ONLY!): ";
}

if (!(cin >> row >> col)) {
cout << "I SAID NUMBERS ONLY!!!!\n";
cin.clear();
cin.ignore(1000, '\n');
continue; // go back to the start of the loop
}
row--; // immediately convert from 1-8 to 0-7
col--;

// Is the number valid?
if (row < 0 || col < 0 || row > 7 || col > 7) {
cout << "Row and column numbers must be 1-8\n";
continue;
}

if (board[row][col] != ' ') {
cout << "That square is already taken\n";
continue;
}

break; // success
}


// Get the value for the square
while (true) {
cout << "Enter S or O(USE CAPITALS ONLY!): ";
cin >> letter;
if (letter == 'S' || letter == 'O') {
break;
} else {
cout << "I SAID CAPITALS ONLY!!!!. ALSO USE S OR O.";
system("pause");
system("cls");
}
}

board[row][col] = letter;
}


void
win1()
{
}


int
main()
{
int row, col;
while (1) {
gameboard();
getmove(row, col);
currentPlayer = !currentPlayer; // switch players

}
return 0;
}


Now it's more readable. Do you want to calculate in the main function or create a separate function to calculate the score?
bro either is acceptablle..but now this code is not working properly..the sos board is out of sync and the counting still doesnot work.
 
.
bro either is acceptablle..but now this code is not working properly..the sos board is out of sync and the counting still doesnot work.
What error you get?

Also since you are using cpp shouldn't you have separate files for headers and implementation?

System.out.println("Java is the best");
I prefer Python..
 
.
What error you get?

Also since you are using cpp shouldn't you have separate files for headers and implementation?


I prefer Python..
it doesnt give an error but now the 8*8 board of sos is spread out of sreen and rows/columns arent visible..the counting still doesnot work..thats the only problem i have.i.e.the counting of scores after each sos is made
 
.
bro either is acceptablle..
Here could be a working algo
1. Create function called score().
2. In that function declare and initialize two variables player1_score and player2_score as 0.
3. Then if player1 wins
player1_score++
print player1_score
else
player2_score++
print player2_score
4. Call score() function from your main function.
 
. .
Here could be a working algo
1. Create function called score().
2. In that function declare and initialize two variables player1_score and player2_score as 0.
3. Then if player1 wins
player1_score++
print player1_score
else
player2_score++
print player2_score
4. Call score() function from your main function.
bro that doesnot work because the score function has to check after every move if an SOS is made and then add +1 to that players score
 
.

Pakistan Defence Latest Posts

Back
Top Bottom