Not saving input and updating.

Pages: 123
I'm pretty sure the problem is because I'm not referencing the input once the function terminates. I'm testing player1 input and whenever player1 inputs X or O the board doesn't update itself. While you're here, some suggestions for improvement is most certainly welcomed.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include "Board.h"
#include <limits>
#include <cctype>

using namespace std;

int main()
{
    // Welcoming message.
    cout << "Welcome users to my very own tic-tac-toe game. " << endl;

    // Object to access the class functions
    Board process;
    char response1; // A variable to gather input from player1 if they want to quit the game or not
    char response2; // A variable to gather input from player2 if they want to quit the game or not
    int tileCounter = 0; // Represents how many tiles have been used.
    int playerWins1 = 0; // How many times player1 won a match.
    int PlayerLose1 = 0; // How many times Player1 lost a match.
    int playerLose2 = 0; // How many times player 2 lost a match.
    int playerWins2 = 0; // How many times player2 won a match.
    int tie = 0;         // How many times a match ended up in a tie.

    while(toupper(response1) != 'N' || toupper(response2) != 'N')
    {
        process.choosingLetter(); // Players choose their letter for the game.
        cout << endl << endl;
        process.draw(); // Creates the board.
        tileCounter = 0; // reinitializes after each game.

        // Game loop
        // If there are no 3 matches, keep playing.
        while(!process.isGameOver(tileCounter,playerWins1,PlayerLose1, playerWins2, playerLose2, tie)) // If there are no 3 matches, keep playing.
        {
            cout << endl;
            process.playerMove1(); // Outputs player1's move.
            cout << endl << endl << endl;
            process.draw(); // Call the board again to see the changes.
            cout << endl;
            tileCounter++;
            if(process.isGameOver(tileCounter,playerWins1,PlayerLose1, playerWins2, playerLose2, tie) == true)
            {
                break;
            }
            process.playerMove2(); // Outputs player2's move.
            cout << endl << endl;
            process.draw();
            tileCounter++;
        }

        process.playAgain(response1, response2);
    }
    process.scoreBoard(playerWins1,PlayerLose1, playerWins2, playerLose2, tie);

    return 0;
}



board.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#ifndef BOARD_H
#define BOARD_H

class Board
{
public:
    Board();
    void draw();
    void choosingLetter();
    void playerMove1();
    void playerMove2();
    void playerInputCheck1();
    void playerInputCheck2();
    void playerLocationCheck1();
    void playerLocationCheck2();
    bool isGameOver(int &, int &, int &, int &, int &, int &);
    void refresh();
    void playAgain(char &, char &);
    void scoreBoard(int &, int &, int &, int &, int &);
private:
    char board[3][3];
    char PlayerLetter1; // The letter that player 1 will be.
    char PlayerLetter2; // The letter that player 2 will be
    std::string playerInput1;  // Input from player1.
    std::string playerInput2;  // Input from player2.
};
Last edited on
Any advice?
add #include <string> at line 3 in board.h
1
2
3
// board.h
// line 3
#include <string> 
Ok, but it didn't really help me.
1
2
3
4
// Objects to access the class functions
   Board letter;
   Board create;
   Board playerMove1;


You create 3 boards here, things affected to one board, does not affect the other.


whenever player1 inputs X or O the board doesn't update itself.


1
2
3
4
5
6
7
8
while(1)
   {
       cout << endl;
       create.initBoard(); // Creates the board.
       cout << endl;
       playerMove1.playerMove1(); // Outputs player1's move
       cout << endl;
   }


In each cycle you re-init the create board, also when you make a move, you never print the board to the screen.



EDIT:

Some improvements:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#ifndef BOARD_H
#define BOARD_H

class Board
{
public:
    Board();
    void initBoard();
    void draw();
    void choosingLetter();
    void playerMove1();
private:
    std::string char board[3][3];
    std::string PlayerLetter1; // The letter that player 1 will be.
    std::string PlayerLetter2; // The letter that player 2 will be
    std::string playerInput1;  // Input from player1.
    std::string playerInput2;  // Input from player2.
};

#endif

Board::Board()
{
   for(unsigned int i = 0; i < 3; i++)
       for(unsigned int j = 0; j < 3; j++)
           board[i][j] = ' ';
}

void Board::draw()
{
    for(unsigned int i = 0; i < 3; i++)
    {
        cout << "\t\t";
        for(unsigned int j = 0;  j < 3;  j++)
        {
            // Display the board.
            cout << "|" << board[i][j] << "|";
        }
        cout << endl; // Create a space between each row and column.
    }

}


It is debatable taking a char over a string, of course not much implementation difference, though I'd wager a plain char array is easier and reduces program size.


Also your loop should also look something like:

loop:
-Draw
-GetInput
-Process
while !Player1Won && !Player2Won
Last edited on
Well the reason I created a string board is because I'm using commands that are strings and I'm pretty sure that those two would conflict with each other.
A letter is a char? I should of changed that too.
If things affected to one board doesn't affect the other, then how I can get around that? What would you do in that situation.
Last edited on
Yes that's what the game loop is suppose to do and I've changed both of the letters to char.
Last edited on
I think the main point is you making 3 boards. Only one board needs to be created and manipulated.

Anyway hope I helped. I'm gonna get some sleep now I'm knackered.
Alright have a good night. I'll see you tomorrow megatron.
Alright megatron I fixed it. Now I learned something new and the code looks much nicer. I'll post it in the main post.
Last edited on
That's great what did you do?
I got rid of those two boards. So I only have one object called "Board process;" that invokes each function and updates it accordingly. Come to think of it, this might solve another problem I had with another program.
Last edited on
Awesome, glad it's fixed. Two players should be easy enough to implement now, with the loop then calling each player function from the board class, and make sure to check whether the space is free or occupied before making a move!

In response to your edit:

Yes, be certain to understand is that a class keeps check of it's own internal variables and can only be accessed through methods or specializations. If you would like help with classes just PM me.
Last edited on
It's ok man you're tired. You can go to sleep lol :)
I mean in general haha, I'm having a smoke before I go to sleep, need to shut my brain up ;)

If you are looking for someone to code with, I'm more than happy too. I have a lot of projects at the moment, for where you are at, you could learn a thing or two quicker with real productivity.
All right, I've updated my main post with the most up-to-date code and the program perfectly gathers input from both player1 and player2 and updates the board accordingly. Now there is two more functions that I need to implement; a bool game over function and a checkboard function. The bool function is obvious enough, and the check functions checks if the field has already begin taken by an X or an O. I'll probably do that tomorrow since it's already night time for me and I'll probably leave this topic open until I finish it.
Last edited on
If you don't might mind me asking, what sort of projects are you working on at the moment. oh, I remember reading a topic saying that you were trying to create an inventory shop. Can you look at mine and tell me what you think?
Last edited on
Sure pm it me dude, I'll be on my laptop hacking away till I KO
Pages: 123