I just finnshed my C book and I don't know what to do with everything I know! Can someone plz find something for me to do?
I just finnshed my C book and I don't know what to do with everything I know! Can someone plz find something for me to do?
Make a cool (not simple and lame) tic tac toe game.
I can't I don't know how to use gfx....
Then do it with text. Try to program in some AI.
Away.
:\ ... I don't know how I'm going to do that...
There are some example of tic-tac-toe code on these forums (where others have posted their problems), try searching some out.
As for the AI part, don't worry about that until you've got the basic game running. Then you can do some more research
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
The AI is stupid, the coordinate system is counter intuitive, and it's in C++, but it should give you a decent idea.
Code:#include <iostream> using namespace std; char matrix[3][3]; /* the tic tac toe matrix */ char check(void); void init_matrix(void); void get_player_move(void); void get_computer_move(void); void disp_matrix(void); int main() { char done; cout << "This is the game of Tic Tac Toe" << endl; cout << "You will be playing against the computer" << endl; done = ' '; init_matrix(); do{ disp_matrix(); get_player_move(); done = check(); /* see if winner */ if(done!= ' ') break; /* winner!*/ get_computer_move(); done = check(); /* see if winner */ system("cls"); } while(done== ' '); system("cls"); disp_matrix(); if(done=='X') cout << "You won!" << endl; else cout << "I won!!!!" << endl; return 0; } /* Initialize the matrix. */ void init_matrix(void) { int i, j; for(i=0; i<3; i++) for(j=0; j<3; j++) matrix[i][j] = ' '; } /* Get a player's move. */ void get_player_move(void) { int x, y; cout << "Enter X,Y coordinates for your move: "; cin >> x >> y; x--; y--; if(matrix[x][y]!= ' '){ cout << "Invalid move, try again." << endl; get_player_move(); } else matrix[x][y] = 'X'; } /* Get a move from the computer. */ void get_computer_move(void) { int i, j; for(i=0; i<3; i++){ for(j=0; j<3; j++) if(matrix[i][j]==' ') break; if(matrix[i][j]==' ') break; } if(i*j==9) { cout << "draw" << endl; exit(EXIT_SUCCESS); } else matrix[i][j] = 'O'; } /* Display the matrix on the screen. */ void disp_matrix(void) { int t; for(t=0; t<3; t++) { printf(" %c | %c | %c ",matrix[t][0], matrix[t][1], matrix [t][2]); if(t!=2) printf("\n---|---|---\n"); } cout << endl; } /* See if there is a winner. */ char check(void) { int i; for(i=0; i<3; i++) /* check rows */ if(matrix[i][0]==matrix[i][1] && matrix[i][0]==matrix[i][2]) return matrix[i][0]; for(i=0; i<3; i++) /* check columns */ if(matrix[0][i]==matrix[1][i] && matrix[0][i]==matrix[2][i]) return matrix[0][i]; /* test diagonals */ if(matrix[0][0]==matrix[1][1] && matrix[1][1]==matrix[2][2]) return matrix[0][0]; if(matrix[0][2]==matrix[1][1] && matrix[1][1]==matrix[2][0]) return matrix[0][2]; return ' '; }
This is an opinion:Originally posted by volk
... and it's in C++...
Is it C++? Just because you have a couple cout and cin statements does not make it a C++ program. It looks like standard C to me.
Make a class, redesign your functions into methods of that class, and you'll be closer.
Walt
cout and cin are classes. That makes it C++. There you have your answer. Furthermore, "<<" and ">>" are over ridden operators. Again, C++ code.Originally posted by WaltP
This is an opinion:
Is it C++? Just because you have a couple cout and cin statements does not make it a C++ program. It looks like standard C to me.
Make a class, redesign your functions into methods of that class, and you'll be closer.
Walt
Just because someone who knows C could understand the code, doesn't mean it "isn't C++".
Quzah.
Hope is the first step on the road to disappointment.
I can accept that definition if you want to be a purist about it. I just question the 'intent' that simply changing a printf into cout converts a program into C++ code. It only requires a C++ compiler but the code written is still C.Originally posted by quzah
cout and cin are classes. That makes it C++. There you have your answer. Furthermore, "<<" and ">>" are over ridden operators. Again, C++ code.
My definition of C++ is a little different. If only I/O is C++ and there are no other classes/methods used (either user-defined or other language-defined) I wouldn't count it as C++, as the above code is written.
In other words, if you teach your cat to bark (output) and eat dog food (input) that doesn't make him a dog. It would have to chase cars, chew bones, and have puppies before I'd call it Rover ;-)
Just an opinion, take it or leave it...
Walt
YAY! The code works! No errors(on my compiler it usally gets errors on code I get off the internet), now it's time to see HOW it works...
...could someone make a C version?
Originally posted by Budgiekarl
I just finnshed my C book and I don't know what to do with everything I know! Can someone plz find something for me to do?Now you make your own C version!Originally posted by Budgiekarl ...could someone make a C version?
The keyboard is the standard device used to cause computer errors!
Actually the check() function has a logical error in it that means it doesn't work like it is meant to. Maybe Budgiekarl could correct this function whilst taking on the task of converting the code to C
DavT
-----------------------------------------------
What worng with check()? I don't see anything... and yes, I will try to convert it(C++ is really close to C!)
Try the moves:
HUMAN: 3 1
COMPUTER: 1 1
HUMAN: 3 2
COMPUTER: 1 2
HUMAN: 3 3 (Human should win at this point)
COMPUTER: 1 3
Computer claims victory (cheeky bugger!) I'll leave it to you to work out why....
DavT
-----------------------------------------------