Board won't work correctly

I'm making a board for a game I am making, and the player won't display or move correctly when I run the program, and I can't find why.

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
57
58
59
60
61
62
63
64
65
66
using namespace std;

char board [21] [8];
char entrance_exit [6];
int monsterColumn [4];
int monsterRow [4];
int playerRow;
int playerColumn;
int playerMove(int (&playerRow), int (&playerColumn));
int moveMonster(int monsterColumn[4],int monsterRow[4]);
int main()
{
    srand(time(NULL));

    do{
        for(int x = 0; x<3; ++x)
        {
            monsterColumn[x] = 0;
            monsterRow[x] = 0;
            monsterRow[x] = rand() % 8;
            monsterColumn[x] = rand() % 21;
        }
    }while(monsterColumn[0] != monsterColumn[1] && monsterColumn[0] != monsterColumn[2] && monsterColumn[0] != monsterColumn[3] && monsterColumn[1] != monsterColumn[2] && monsterColumn[1] != monsterColumn[3] && monsterColumn[2] != monsterColumn[3] && monsterRow[0] != monsterRow[1] && monsterRow[0] != monsterRow[2] && monsterRow[0] != monsterRow[3] && monsterRow[1] != monsterRow[2] && monsterRow[1] != monsterRow[3] && monsterRow[2] != monsterRow[3]);

    entrance_exit[1] = 'O';
    do{
        board [playerColumn] [playerRow] = 'O';
        cout << " ----------"<<entrance_exit[3]<<entrance_exit[4]<<entrance_exit[5]<<"----------\n";
        for (int x = 0; x<8; ++x)
        {
            cout << " |";
            for (int y = 0; y<21; ++y)
            {
                cout << board [x] [y];
            }
            cout <<  "|\n";
        }
        cout << " ----------"<<entrance_exit[0]<<entrance_exit[1]<<entrance_exit[2]<<"----------\n";
        board [playerColumn] [playerRow] = ' ';
        playerMove(playerColumn, playerRow);
    }while(playerColumn < 9);
return 0;
}

int playerMove(int (&playerColumn), int (&playerRow))
{
    char playerMoveChoice;
    cin >> playerMoveChoice;
    if (playerMoveChoice == 'W' || playerMoveChoice == 'w')
    {
        ++playerRow;
    }
    else if (playerMoveChoice == 'A' || playerMoveChoice == 'a')
    {
        --playerColumn;
    }
    else if (playerMoveChoice == 'S' || playerMoveChoice == 's')
    {
        --playerRow;
    }
    else
    {
        ++playerColumn;
    }
    return 0;
}
Last edited on
Yes. Recall your first programs - how did cin work like?

You have to press enter for input to be processed - until you press enter, it's just there, no one touches it. You want player to move immediately after key being pressed - so no room for enter!

You could decide "Okay then, I need a function that will detect just that!" - that would leave you to kbhit() and getch(), if I remember them correctly(haven't been using them for quite some time).
But that still yields a problem with outputting data - you'd probably prefer board to be static, and update instead of being printed each time.

Therefore, you could now go in two different directions. First one would be to get familiar with windows library, which has some functions for manipulating console output. This, however, is OS-dependent. If you choose to change your OS(or aren't using Windows), you won't be able to get this functionality without rewriting some code.

Better approach, imho, would be to look for an external library, that would happen to be multiplatform. Such is "Curses" library(probably called nCurses on Windows). It's written in C, so you may feel strange at first(functions to write some data look like C's printf then C++'s cout, same goes for cin). However, it's good library for doing any GUI in console - it's popular for reason. If I were you, I'd consider picking it up.

Cheers!
I've been fooling around with getch() and I can't figure out how to get it to work right, could you please explain what it does?
1
2
char ch;
ch = _getch();


Unlike cin that requires the enter key pressed, getch will return as soon as you press a key. In the above, ch will hold key pressed.


When I was testing it, I was just inputting into a variable and then outputting it and it was outputting the ASCII numerals for things. At least I think it was.
Last edited on
Topic archived. No new replies allowed.