help with simple game

NOTE - I have designed it wrong it turned into a tic toc tac game with 4 values = to win

I have designed a simple 4 - CONNECT game with infinty board,
u can insert the coordinates of the board.

then u can enter a value that represent a value on the board
then the progream translate it to x and y coordinates
and assign it to an array so it changes it from an number to an 'r' or 'b'
(red or blue)

the question I am asking is : the equations I used to turn value to x and y coordinates not working correctly here is the equations.



1
2
3
4
                      ((input + (x+1)) / x) - 1;
                      truey = (input % y) - 1;
                      if(input % y == 0){
                      truey = (input -1)%y;}


so if someone kind enough , can u correct the equations .

here is the code if u don't understand me , just run .

some variables I used have weird name like pepega just read the rest of the variable and u will understand.
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

[code]
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

//functions
void pepegaquarters(std::string position[][99] ,int x , int y , std::string choice);
void infintyikea(int x , int y , std::string  position[][99]);
bool checkposition(int x , int y, std::string position[][99], int truex , int truey);
void pepegaassign(int truex, int truey , std::string position[][99] , std::string choice , int i) ;
bool wincon(std::string position[][99] , int x ,int  y ,int  stepx , int stepy);

int main()
{
  int realx,realy,boardcounter,truex,truey,pepegainput = 1;
  std::string position[99][99] , choice;
  std::cout << "                                     WELCOME TO 4- CONNECT          \n                                _______________________________\n";
  boardcounter = 1;
  std::cout << "can u tell us about the rows of the board : ";
  std::cin >> realx;
  std::cout << "and the coloumns pls: ";
  std::cin >> realy;
  std::cout << "\n";
  for(int i = 0; i < realx ; i++ ){
      for(int j = 0 ; j < realy ; j++){
          position[i][j] = std::to_string(boardcounter);
          boardcounter++;
      }
  }


  infintyikea(realx , realy , position);
  std::cout << "\ncan u pls input the color :";
  std::cin >> choice;
  std::cout << "\n";
  pepegaquarters(position , realx , realy , choice);
  std::cin.ignore();
  std::cin.get();
}






void pepegaquarters(std::string position[][99], int x , int y , std::string choice)
{
  if(choice != "red" && choice != "blue")
          std::cout << "\nERROR";
  else {
      int truex , truey , pepegainput;
      int i = 0;
      for( ; ;){





          system("CLS");
          //ask the user
          if(i == 0)
              std::cout << "1st player pls input (number in the board) ";
          else if (!(i % 2 == 0)){
              std::cout << "2ed player pls input ";
          }
          else if (i % 2 == 0){
              std::cout <<"1st player pls input  ";

          }
          std::cout << "\n\n";

          infintyikea(x , y ,position);
          std::cout << "\n:";
          std::cin >> pepegainput;
             //big brain equations
          truex = ((pepegainput + (x+1)) / x) - 1;
          truey = (pepegainput % y) - 1;
          if(pepegainput % y == 0){
          truey = (pepegainput -1)%y;}










          if(checkposition(x , y ,position , truex , truey) == true){
                      pepegaassign(truex , truey , position , choice , i);




          }
          else if (checkposition(x , y ,position , truex , truey) != true){
              std::cout << "\n EROOR";
          }
          if(wincon(position , x , y , 0 , 1) || wincon(position , x , y , 1 , 0)  || wincon(position , x , y , 1 , 1)  || wincon(position , x , y , 1 , -1) == true ){
              std::cout << "\n";
              if(i % 2 == 0)
                  std::cout <<"congrats first player won !!!";
              else if(!(i% 2 == 0))  {
                  std::cout << "congrats 2ed player won !!!";}
              break;


          }

      i++;
      }

  }

}









void infintyikea(int x , int y , std::string position[][99])
{

  for(int i = 0 ; i < x ; i++){

      for( int j = 0 ; j < y ; j++){
          std::cout << position[i][j];

          // weird useless sht

          std::stringstream geek(position[i][j]);
          int z = 0;
          geek >> z;
          if(position[i][j] == "r"||position[i][j] == "b" && i*z > 9){
              std::cout << "  |  ";
          }
          else if(z > 99 ){
              std::cout << "|  ";
          }
          else if(z > 9){
              std::cout << " |  ";
          }
          else{
          std::cout << "  |  ";}

      }
      std::cout << "\n";

  }
}
bool checkposition(int x , int y , std::string position[][99], int truex , int truey)

{
  int trueboard;
  trueboard = truex * truey;

  if(truex > x || truey > y )
      return false;
  else if(position[truex][truey] == "R" || position[truex][truey] == "B")
      return false;
  else
      return true;
}
void pepegaassign(int truex , int truey , std::string position[][99] , std::string choice , int i)
{
  if(choice == "red"){
      if(i % 2 == 0)
          position[truex][truey] = "r";
      else if (!(i % 2 == 0))
          position[truex][truey] = "b";


      }
  if(choice =="blue"){
       if(i % 2 == 0)
          position[truex][truey] = "b";
      else if (!(i % 2 == 0))
          position[truex][truey] = "r";

  }


}

bool wincon(std::string position[][99] ,int  x ,int  y ,int  stepx ,int  stepy)
{
  for(int i = 0 ;i < x ; i++ ){
      for(int j = 0 ; j < y ; j++){
              std::string startvalue = position[i][j];
              for (int b = 1; b < 4 ; ++b){
                      if( position[i+ b*stepx][j+ b*stepy] != startvalue){
                          return false;
                      }



              }
      return true;

  }
}
}

Last edited on
1. Formatting - if your code is a mess, then it's hard to figure out what the overall flow is like.
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
#include <iostream>
#include <string>
#include <sstream>
#include <cstdlib>

//functions
void pepegaquarters(std::string position[][99], int x, int y, std::string choice);
void infintyikea(int x, int y, std::string position[][99]);
bool checkposition(int x, int y, std::string position[][99], int truex, int truey);
void pepegaassign(int truex, int truey, std::string position[][99], std::string choice, int i);
bool wincon(std::string position[][99], int x, int y, int stepx, int stepy);

int main()
{
  int realx, realy, boardcounter, truex, truey, pepegainput = 1;
  std::string position[99][99], choice;
  std::
      cout <<  "        WELCOME TO 4- CONNECT\n"
               "        _____________________\n";
  boardcounter = 1;
  std::cout << "can u tell us about the rows of the board : ";
  std::cin >> realx;
  std::cout << "and the coloumns pls: ";
  std::cin >> realy;
  std::cout << "\n";

  for (int i = 0; i < realx; i++) {
    for (int j = 0; j < realy; j++) {
      position[i][j] = std::to_string(boardcounter);
      boardcounter++;
    }
  }

  infintyikea(realx, realy, position);
  std::cout << "\ncan u pls input the color :";
  std::cin >> choice;
  std::cout << "\n";
  pepegaquarters(position, realx, realy, choice);
  std::cin.ignore();
  std::cin.get();
}

void pepegaquarters(std::string position[][99], int x, int y, std::string choice)
{
  if (choice != "red" && choice != "blue")
    std::cout << "\nERROR";
  else {
    int truex, truey, pepegainput;
    int i = 0;
    for (;;) {
      system("CLS");
      //ask the user
      if (i == 0)
        std::cout << "1st player pls input (number in the board) ";
      else if (!(i % 2 == 0)) {
        std::cout << "2ed player pls input ";
      } else if (i % 2 == 0) {
        std::cout << "1st player pls input  ";
      }
      std::cout << "\n\n";
      infintyikea(x, y, position);
      std::cout << "\n:";
      std::cin >> pepegainput;

      //big brain equations
      truex = ((pepegainput + (x + 1)) / x) - 1;
      truey = (pepegainput % y) - 1;
      if (pepegainput % y == 0) {
        truey = (pepegainput - 1) % y;
      }

      if (checkposition(x, y, position, truex, truey) == true) {
        pepegaassign(truex, truey, position, choice, i);
      } else if (checkposition(x, y, position, truex, truey) != true) {
        std::cout << "\n EROOR";
      }

      if ( wincon(position, x, y, 0, 1) ||
           wincon(position, x, y, 1, 0) ||
           wincon(position, x, y, 1, 1) ||
           wincon(position, x, y, 1, -1) == true) {
        std::cout << "\n";
        if (i % 2 == 0)
          std::cout << "congrats first player won !!!";
        else if (!(i % 2 == 0)) {
          std::cout << "congrats 2ed player won !!!";
        }
        break;
      }

      i++;
    }
  }
}

void infintyikea(int x, int y, std::string position[][99])
{
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      std::cout << position[i][j];

      // weird useless sht
      std::stringstream geek(position[i][j]);
      int z = 0;
      geek >> z;
      if (position[i][j] == "r" || position[i][j] == "b" && i * z > 9) {
        std::cout << "  |  ";
      } else if (z > 99) {
        std::cout << "|  ";
      } else if (z > 9) {
        std::cout << " |  ";
      } else {
        std::cout << "  |  ";
      }
    }
    std::cout << "\n";
  }
}

bool checkposition(int x, int y, std::string position[][99], int truex, int truey)
{
  int trueboard;
  trueboard = truex * truey;
  if (truex > x || truey > y)
    return false;
  else if (position[truex][truey] == "R" || position[truex][truey] == "B")
    return false;
  else
    return true;
}

void pepegaassign(int truex, int truey, std::string position[][99], std::string choice, int i)
{
  if (choice == "red") {
    if (i % 2 == 0)
      position[truex][truey] = "r";
    else if (!(i % 2 == 0))
      position[truex][truey] = "b";
  }
  if (choice == "blue") {
    if (i % 2 == 0)
      position[truex][truey] = "b";
    else if (!(i % 2 == 0))
      position[truex][truey] = "r";
  }
}

bool wincon(std::string position[][99], int x, int y, int stepx, int stepy)
{
  for (int i = 0; i < x; i++) {
    for (int j = 0; j < y; j++) {
      std::string startvalue = position[i][j];
      for (int b = 1; b < 4; ++b) {
        if (position[i + b * stepx][j + b * stepy] != startvalue) {
          return false;
        }
      }
      return true;
    }
  }
}


2. Warnings - enable maximum warnings in your compiler.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ g++ -std=c++11 -Wall baz.cpp
baz.cpp: In function ‘int main()’:
baz.cpp:15:35: warning: unused variable ‘truex’ [-Wunused-variable]
   int realx, realy, boardcounter, truex, truey, pepegainput = 1;
                                   ^
baz.cpp:15:42: warning: unused variable ‘truey’ [-Wunused-variable]
   int realx, realy, boardcounter, truex, truey, pepegainput = 1;
                                          ^
baz.cpp:15:49: warning: unused variable ‘pepegainput’ [-Wunused-variable]
   int realx, realy, boardcounter, truex, truey, pepegainput = 1;
                                                 ^
baz.cpp: In function ‘void infintyikea(int, int, std::__cxx11::string (*)[99])’:
baz.cpp:106:58: warning: suggest parentheses around ‘&&’ within ‘||’ [-Wparentheses]
       if (position[i][j] == "r" || position[i][j] == "b" && i * z > 9) {
                                                          ^
baz.cpp: In function ‘bool checkposition(int, int, std::__cxx11::string (*)[99], int, int)’:
baz.cpp:122:7: warning: variable ‘trueboard’ set but not used [-Wunused-but-set-variable]
   int trueboard;
       ^
baz.cpp: In function ‘bool wincon(std::__cxx11::string (*)[99], int, int, int, int)’:
baz.cpp:161:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^

baz.cpp:106 and baz.cpp:161 have the potential to mess with your mind.

3. Prototyping.
If some bit of logic is confusing, then make a side project prototype.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main ( ) {
  int x = 3, y = 4;
  do {
    int input, truex, truey;
    cin >> input;
    ((input + (x+1)) / x) - 1;
    truey = (input % y) - 1;
    if(input % y == 0){
    truey = (input -1)%y;}

    cout << "X=" << x << endl;
    cout << "Y=" << y << endl;
    cout << "input=" << input << endl;
    cout << "truex=" << truex << endl;
    cout << "truey=" << truey << endl;
  } while ( true );
}

You can rapidly edit and test small bits of code like this until you understand the logic.
Then you can incorporate that into your larger program.
yeah I the day I posted this post , I debuged my code until I found my game logic was a big dumb mess , so I fixed it and the logic worked so ty for replaying .
Topic archived. No new replies allowed.