Need some help writing a game code

Update March 9th: I think i am finally getting somewhere with this program still a lot of mistakes. Thanx for all the help really appreciate it. Will update op with my code soon
Last edited on
Bump. Still trying but anything helps :)
you would need two inputs from the user. the position where they want to place the number and the actual value of the number.

so example user 1 enters 1 for the position it will map to grid[0][0] and enter 9 so you fill grid[0][0] with 9.

you also need to do some checking if the number has already been used.

i would store the numbers in a vector and then remove it once its used.
if user inputs a value that is not in your storage report an error.

this game is a bit complex. you need to break it down into small steps.

you would need a while loop to repeat the whole process of
getting input from user -
checking move was valid -
redrawing your board
check for win condition.

this should get you started.


Ok i'm at work I'll get some more done tonight and then update the post. Thanks a lot.
Let us play it after you finish. It seems to be interesting. :D
BTW. the operator for input is >> not << like on line 24: cin << num1 << endl;
I do not know so well C++ but I thing you wrote code wrong. Primarily need creat matrix
1
2
3
4
5
6
7
8
9
10
11
const int row=3,col=3;
	int mtx[row][col]={0,1,2,3,4,5,6,7,8};
for(int i=0;i<row;i++)
{
	for(int j=0;j<col;j++)
	{
		
		mtx[i][j];
	}
	cout<<endl;
}


then need creat loop "player1" & "player2"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int num;
string s[2]={"player1 enter number between 1&9","player2 enter number between 1&9"};

for(int i=1;i<=4;i++)
{
cout<<s[0]<<endl;
cin>>num;
	for(int j=1;j<=5;j++)
	{
		cout<<s[j%2]<<endl;
		cin>>num;
	}
	break;
}

after you must assay equal sum number
1
2
3
4
5
6
7
8
9
if(15==sum)
{
	cout<<"you win";
}
else if(15!=sum)
{
	cout<<"your sum number "<<sum<<"you loser";

}

join this blocks and finalize code
Last edited on
Ok I used everyones suggestions and came up with another draft!
p.s. I tried to use the vectors and it didn't really work out (Didn't learn much about vectors yet) so I am still trying to implement vectors into my code, slowly learning. More feedback would be greatly appreciated. Thank you for all the help.
Bump still looking for some advice on my updated code
Last edited on
Can you post your updated code and tell us what problem is there ?
[code]
#include <iostream>
#include <play.h>
#include <check.h>

Last edited on
I have started a section of the game loop for you. hope you can follow the example.
I will build up more sections when I get the time to do it.

I have noticed in your check for win condition function.
you need to sum up the numbers instead of &&

 
  if ((grid[1] + grid [2] + grid [3]) == 15)


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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

vector<int> matrix_options;
vector<int> box_options;
int matrix[3][3];

// still need to implement which player made the turn.
// x for player 1
// o for player 2

//   1x   |     2o  |   3
// [0][0] |  [0][1] | [0][2]
//________|_________|_______
//    4   |     5   |   6
// [1][0] |  [1][1] | [1][2]
//________|_________|_______
//    7   |     8   |   9
// [2][0] |  [2][1] | [2][2]
//        |         |

void display_matrix()
{
    cout << endl;

    cout << " " << matrix[0][0]  << " | " << matrix[0][1] << " | " << matrix[0][2]  << endl;
    cout << "------------" << endl;
    cout << " " << matrix[1][0]  << " | " << matrix[1][1] << " | " << matrix[1][2]  << endl;
    cout << "------------" << endl;
    cout << " " << matrix[2][0]  << " | " << matrix[2][1] << " | " << matrix[2][2]  << endl;

}



void populate_selection()
{
    for(int i=1; i < 10; i++)
    {
        box_options.push_back(i);
        matrix_options.push_back(i);
    }
}

void display_options(vector<int>& storage)
{

    // display list of available options
    for(int i=0; i < storage.size(); i++)
    {
        cout << storage[i] << " ";
    }

    cout << endl;
}

// first argument is passed by reference
// second argument is passed by value
void remove_option(vector<int>& storage, int value)
{
    // removes a value from storage
    auto it = std::find(storage.begin(), storage.end(), value);

    if(it != storage.end())
    {
        storage.erase(it);
    }
}

void fill_matrix(int position, int value)
{
    switch(position)
    {
        case 1:
        {
            matrix[0][0] = value;
        }
        break;

        case 2:
        {
            matrix[0][1] = value;
        }
        break;

        case 3:
        {
            matrix[0][2] = value;
        }
        break;

        case 4:
        {
            matrix[1][0] = value;
        }
        break;

        case 5:
        {
            matrix[1][1] = value;

        }
        break;

        case 6:
        {
            matrix[1][2] = value;

        }
        break;

        case 7:
        {
            matrix[2][0] = value;
        }
        break;

        case 8:
        {
            matrix[2][1] = value;
        }
        break;

        case 9:
        {
            matrix[2][2] = value;
        }
        break;
    }

    system("cls");
    display_matrix();

}

int main(int argc, char *argv[])
{

    populate_selection() ;

    cout << "Welcome to tic-tac-math" << endl;
    cout << "The goal of this game is to get the sum of 15 in either a row, column, or diagonal." << endl;
    cout << " You can only use numbers between 1 and 9" << endl;

    bool game_active = true;

    int x = 0;
    int y = 0;

    // start of game loop
    while(game_active)
    {
        cout << "remaining options for matrix" << endl;
        display_options(matrix_options);
        cout << "Enter Matrix position: ";

        while(!(cin >> x) || (x > 9) || (x < 1))
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input.  Try again: ";
        }

        if ( std::find(matrix_options.begin(), matrix_options.end(), x) != matrix_options.end() )
        {
            cout << "found " << x << endl;
            remove_option(matrix_options,x);
            cout << "remaining options for matrix" << endl;
            display_options(matrix_options);

            cout << "remaining options for box" << endl;
            display_options(box_options);
            cout << "Enter Box value: ";


            while(!(cin >> y) || (y > 9) || (y < 1))
            {
                cin.clear();
                cin.ignore(numeric_limits<streamsize>::max(), '\n');
                cout << "Invalid input.  Try again: ";
            }

            if ( std::find(box_options.begin(), box_options.end(), y) != box_options.end() )
            {
                cout << "found " << y << endl;
                remove_option(box_options,y);
                cout << "remaining options for box" << endl;
                display_options(box_options);

            }
            else
            {
                cout << "invalid selection " << y << endl;
                display_options(box_options);

            }

        }
        else
        {
            cout << "invalid selection " << x << endl;
            display_options(matrix_options);

        }

        fill_matrix(x,y);

    }
}
Last edited on
what are your rules for the win condition?

it doesn't look very exciting if its just a sum of 15 because a player can win in exactly 2 turns.
I want to make it more exciting but trying to make it work first. My problem now is that My matrix prints but it is not updating with the number that the user is printing.
Last edited on
Have you tried the example i've showed you?

Can you post the updated source code that you are using.

Topic archived. No new replies allowed.