What's new

C++;Help needed

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.
Actually you can do it in your win1() function.

Can you tell me how mathematically the score should be counted?
 
Actually you can do it in your win1() function.

Can you tell me how mathematically the score should be counted?
After every move the function should check if an SOS is made,if thats the case u add +1 to that player.if not then nothing happens.
A friend suggested something like this using loops.but i dont know how and where to implement in code:

int newSOSes(int row, int col);
After each move you will adjust the score accordingly. Something like:

int count = newSOSes(row, col);
if (player <= 0) {
score1 += count;
} else {
score2 += count;
}
 
Use string for player name and int for score.


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.
Print works in Python.
 
After every move the function should check if an SOS is made,if thats the case u add +1 to that player.if not then nothing happens.
A friend suggested something like this using loops.but i dont know how and where to implement in code:

int newSOSes(int row, int col);
After each move you will adjust the score accordingly. Something like:

int count = newSOSes(row, col);
if (player <= 0) {
score1 += count;
} else {
score2 += count;
}
PHP:
void win1()
{
  
        if(board[0][0] == 'S' && board[0][1] == 'O' && board[0][2] == 'S')//horizontal 1,2,3
    {
        score1++;
    }
void win2()
{
  
    if(board[0][0] == 'S' && board[0][1] == 'O' && board[0][2] == 'S')//horizontal 1,2,3
    {
        score2++;
    }
Something like this?

Then change you getmove()function

PHP:
void getmove()
{
    int row, col;
    if(currentPlayer ==0)
    {
       
        while(!(cin>>a))
        {
            cout<<"I SAID NUMBERS ONLY!!!!";
            cin.clear();
            cin.ignore(1000,'\n');
        }
       
        cin>>letter;
        if(letter == 'S' || letter == 'O')
        {
            row = (a-1)/8;
            col = (a-1)%8;
            board[row][col] = letter;
            player++;
            gameboard();
            win1();
           
           
           
        }
   
        else
        {
           
            system("pause");
            system("cls");
        }
       
    }
   
    else
    {
        cout<<"P2 enter the block(NUMBERS ONLY!): ";
        cin>>a;
        cout<<"Enter S or O(USE CAPITALS ONLY!): ";
        cin>>letter;
        if(letter == 'S' || letter == 'O')
        {
            row = (a-1)/8;
            col = (a-1)%8;
            board[row][col] = letter;
            player--;
            win2();
           
           
           
        }
        else
        {
           
            system("cls");
        }
       
       
    }
}

Print works in Python.
I was giving the algo not the actual code.
 
PHP:
void win1()
{
 
        if(board[0][0] == 'S' && board[0][1] == 'O' && board[0][2] == 'S')//horizontal 1,2,3
    {
        score1++;
    }
void win2()
{
 
    if(board[0][0] == 'S' && board[0][1] == 'O' && board[0][2] == 'S')//horizontal 1,2,3
    {
        score2++;
    }
Something like this?

Then change you getmove()function

PHP:
void getmove()
{
    int row, col;
    if(currentPlayer ==0)
    {
      
        while(!(cin>>a))
        {
            cout<<"I SAID NUMBERS ONLY!!!!";
            cin.clear();
            cin.ignore(1000,'\n');
        }
      
        cin>>letter;
        if(letter == 'S' || letter == 'O')
        {
            row = (a-1)/8;
            col = (a-1)%8;
            board[row][col] = letter;
            player++;
            gameboard();
            win1();
          
          
          
        }
  
        else
        {
          
            system("pause");
            system("cls");
        }
      
    }
  
    else
    {
        cout<<"P2 enter the block(NUMBERS ONLY!): ";
        cin>>a;
        cout<<"Enter S or O(USE CAPITALS ONLY!): ";
        cin>>letter;
        if(letter == 'S' || letter == 'O')
        {
            row = (a-1)/8;
            col = (a-1)%8;
            board[row][col] = letter;
            player--;
            win2();
          
          
          
        }
        else
        {
          
            system("cls");
        }
      
      
    }
}


I was giving the algo not the actual code.
Thanks bro for your help.......
 
This seems like C and not C++. Anyway learn your stuff and do it yourself.
When programming create the simplest form of what you want to achieve and then build upon it/expand it. If it ain't working keep trying until it works. Learn to plan/manage your workload properly as you will need this skill in Industry.
 
This seems like C and not C++. Anyway learn your stuff and do it yourself.
It's C++.
cin, cout are cpp functions. Although he is coding in basic C style putting everything in his main.cpp file, I think?
 
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 using function programs in php for fancy back end work on websites but its been months and i dont remember them after leaving the software house for a completely different job months ago
 
Programming not my cup of tea.:coffee:.. gave up programming long time back....
 
Back
Top Bottom