Help! Console is trolling me?

I am trying to make a connect four game before waving goodbye to programming in the console. But what I now notice is that when I try to update positions on the board, it just won't update it as I request. It keeps updating the same spot over and over again:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

void timer(unsigned short i){
clock_t delay = i*CLOCKS_PER_SEC;
clock_t start = clock();
    while(clock() - start < delay);
}

class ConnectFour{
public:
    ConnectFour(){markerX = 'X', markerY = 'Y';}
    void assignBoard(char multiArray[][7]){
        for(int r=0; r<6; r++){
            for(int c=0; c<7; c++){
                multiArray[r][c] = 'O';
            }
        }
    }
    void displayBoard(char multiArray[][7]){
        for(int r=0; r<6; r++){
            cout << "           ";
            for(int c=0; c<7; c++){
                cout << multiArray[r][c] << "|";
            }
            cout << endl;
        }
        cout << "           " << "1 2 3 4 5 6 7" << endl;
    }
    void turn(unsigned short& loop){
        cout << endl;
            if(loop%2 == 0)
                cout << "            " << "Player X: ";
            else
                cout << "            " << "Player Y: ";
    }
    void errHandle(int& value){
        if(!cin){
            cout.put('\a');
            system("cls");
            system("color 1A");
            cout << "ERROR: Non-integer value has been registered!\n\nTERMINATING\n";
            exit(EXIT_FAILURE);
        }
        else if(value <= 0 || value > 7){
            system("cls");
            cout << "Please only enter values between 1 and 7\n";
            timer(2);
            system("cls");
        }
    }
    void pos(char multiArray[][7], int& value, unsigned short& loop){
        switch(value){
            case 1:
                if(multiArray[5][0] != markerX || multiArray[5][0] != markerY){
                    if(loop%2 == 0)
                        multiArray[5][0] = markerX;
                    else
                        multiArray[5][0] = markerY;
                }
                else if(multiArray[5][0] == markerX && multiArray[4][0] != markerX || multiArray[5][0] == markerY && multiArray[4][0] != markerY){
                    if(loop%2 == 0)
                        multiArray[4][0] = markerX;
                    else
                        multiArray[4][0] = markerY;
                }
                else if(multiArray[4][0] == markerX && multiArray[3][0] != markerX || multiArray[4][0] == markerY && multiArray[3][0] != markerY){
                    if(loop%2 == 0)
                        multiArray[3][0] = markerX;
                    else
                        multiArray[3][0] = markerY;
                }
                else if(multiArray[3][0] == markerX && multiArray[2][0] != markerX || multiArray[3][0] == markerY && multiArray[2][0] != markerY){
                    if(loop%2 == 0)
                        multiArray[2][0] = markerX;
                    else
                        multiArray[2][0] = markerY;
                }
                else if(multiArray[2][0] == markerX && multiArray[1][0] != markerX || multiArray[2][0] == markerY && multiArray[1][0] != markerY){
                    if(loop%2 == 0)
                        multiArray[1][0] = markerX;
                    else
                        multiArray[1][0] = markerY;
                }
                else if(multiArray[1][0] == markerX && multiArray[0][0] != markerX || multiArray[1][0] == markerY && multiArray[0][0] != markerY){
                    if(loop%2 == 0)
                        multiArray[0][0] = markerX;
                    else
                        multiArray[0][0] = markerY;
                }
        }
    }
private:
    char markerX, markerY;
};

main(){
char board[6][7];
unsigned short loop = 0;
int value;
ConnectFour game;
game.assignBoard(board);
    while(loop < 42){
        game.displayBoard(board);
        game.turn(loop);
        (cin >> value).get();
        game.errHandle(value);
            if(value <= 0 || value > 7)
                continue;
        game.pos(board, value, loop);
        loop++;
        system("cls");
    }
}
What is switch in line 55 supposed to do? It only work if you enter "1"? Where are the other cases?

As additional suggestion: make errHandle() return true or false, so you could avoid checking the same condition in lines 110 and 47.

1
2
if( game.errHandle(value) )
    continue;
Thanks, but that's not relevant to my question here ;)

I need some kind of hint or solution as to why my "pos" function does not work properbly. There is something wrong with my 'if' statements, just don't know why or what?!

EDIT:
I know that it only works if you input 1. But I am not gonna continue wasting time on creating the rest if it's not working optimal anyways.

It should stack up when you type '1', but it doesn't. It overwrites the bottom instead.
Last edited on
Sorry, for not relevant answer before.
http://en.wikipedia.org/wiki/Don%27t_repeat_yourself
http://en.wikipedia.org/wiki/KISS_principle

You do not write programs in a way that requires to manually check all permutations of conditions. Let the computer do it for you. In your code the line 57 is broken. Read it carefully, and try to find a value that is NOT different from both X and Y. It would have to be equal to them both. The condition is always true.
I finnished the function for you. It got reduced to just these few lines:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void pos(char multiArray[][7], int& value, unsigned short& loop){
	//Decide whose turn is in one check at the start
	char currentPlayerMarker;
	if(loop%2 == 0)
		currentPlayerMarker = markerX;
	else 
		currentPlayerMarker = markerY;
	// Simplyfy condition - yours was broken - always true.
	// Drop the switch, use 'value' to address the right column
	// Don't case each possible variant manually - loop through them
	for(int row = 5; row >=0; row--){
		if(multiArray[row][value-1] == 'O'){
			multiArray[row][value-1] = currentPlayerMarker;
			break;
		}
	}
}

Last edited on
Topic archived. No new replies allowed.