tic tac toe.

Pages: 12
thank you guys.
Very nice work :)

I have a few things you can consider for a future program.

First, use the pre-processor directive #define . This will define an identifier to a value for your entire program without having to make a global variable.

For example:
1
2
3
4
5
6
//  put these after the includes (#include <iostream>)
#define CIRCLE 'O'
#define X  'X'

// Line 32 of your program could be:
mark=(player==1)? X : CIRCLE;


The advantage here is that you didn't make a global variable (no RAM used), but you do have a global constant.

Another thing for tough if/else statements is a sort of binary method. Consider the first 3 squares in the board (squares[1] through squares[3])

1
2
3
4
5
6
7
8
9
10
int state = 0;
if (sqaures[1] == 'X') state += 1;
if (sqaures[2] == 'X') state += 2;
if (sqaures[3] == 'X') state += 4;

// we've made state have a different value for every possibility
// for example:

if (state == 3 || state == 5 || state == 6)
  cout << "X is close to winning, better be careful" << endl;


Do you get that last part? If 1 and 2, but not 3 == 'X' then state will be 3. There is no other way to make state == 3. You've made state equal a number 0-7 and every number is a different combination of what 'X' is doing in the first row. If state == 7 then X won.

To add more: if (sqaures[4] == 'X') state += 8;. Now many more possibilities.
Last edited on
thank you for your good words about my program.
i am begginer yet,so if you wanna you can add me here:magas1291
it is my skype.
Topic archived. No new replies allowed.
Pages: 12