-
TicTacToe cin problem
I just started this program so it does really do anything yet.
Code:
/*
**************************************************
***TicTacToe.cpp Program ***
***This is Tic-Tac-Toe. Get 3 in a row in win! ***
***Author: CPTR Class 212 ***
***Steven Davis ***
**************************************************
*/
#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;
void displayBoard();
char makeUpperCase(char p1);
int main ()
{
string
p1Name,
p2Name;
char
p1,
p2;
int error = 0;
cout << "***TIC-TAC-TOE***\n";
displayBoard();
system("pause");
system("cls");
cout << "Player One, Enter your name: ";
cin >> p1Name;
cout << "\nPlayer Two, Enter your name: ";
cin >> p2Name;
do
{
cout << p1Name << ", would you like to be X's or O's?\n";
/*
Here is my problem. If I hit x or X it works but if i hit anything else
it lets me know there was an error and starts the loop over.
When ever the loop is restarted and it asks me to enter in X or O
I cant. If i hit x or X it just skips it like i didnt enter anything. I
tried to do a cin.clear and cin.ignore but they dont seem to fix
them problem. TIA :)
*/
cin >> p1;
p1 = makeUpperCase(p1);
if (p1 == 'X')
{
p2 = 'O';
}
else if (p1 == 'O')
{
p2 = 'X';
}
else
{
error = 1;
cin.clear();
cin.ignore(1024, '\n');
cout << "\nERROR: Please choose X or O!\n";
}
} while (error == 1); //End While loop for X's and O's
cout << p1Name << " is " << p1 << endl
<< p2Name << " is " << p2 << endl;
return 0;
} //End Main
void displayBoard()
{
char rowOne[10][13] = {
{' ',' ',' ','A',' ',' ',' ','B',' ',' ',' ','C',' '},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{'1',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{' ',' ','_','_','_','|','_','_','_','|','_','_','_'},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{'2',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{' ',' ','_','_','_','|','_','_','_','|','_','_','_'},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{'3',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
{' ',' ',' ',' ',' ','|',' ',' ',' ','|',' ',' ',' '},
};
for (int i=0; i < 10; i++)
{
for (int j=0; j < 13; j++)
{
cout << rowOne[i][j];
}//End for loop
cout << endl;
}//End for loop
}// End Display Board
char makeUpperCase(char p1)
{
char uppercase;
switch (p1)
{
case 'x':
uppercase = 'X';
break;
case 'o':
uppercase = 'O';
break;
case 'a':
uppercase = 'A';
break;
case 'b':
uppercase = 'B';
break;
case 'c':
uppercase = 'C';
break;
default:
return p1;
};
return uppercase;
}// End Make Upper Case
-
The bug is that you never reset error to zero. Display board is also not the way I would do things, but I find it very pretty.
-
Well, just kind of curious, how would you display the board. I figured if i made a function to display it, it would make things easier. As in, i would not have to create the board everytime a move is made. It seemed the easiest and best way. Well for my knowledge it did any way :)
[EDIT]
Sorry, I forgot to thank you for the help.
THANK YOU VERY MUCH!! :D
[/EDIT]
-
I would probably go with a board based on char board[9]; and fill each char with a digit from 1 to 9 then when I want to display I could do.
Code:
class board {
char cells[9];
public:
board() {for(int i=0;i<9;++i) cells[i] = '1' + i;}
std::ostream & display(std::ostream &os = std::cout) {
for(int row = 0; row < 3;++row) {
os << cells[row*3] << '|' << cells[row*3+1] << '|' << cells[row*3+2] << "\n- - -\n";
}
}
bool is_blank(int x, int y) const {
if(x>=0 && x <3 && y >=0 && y < 3) {
return cells[y*3+x] >= '1' && cells[y*3+x] <= '9';
} else return false;
}
bool set(int x, int y, char c) {
if(is_blank(x,y)) {cells[y*3+x] = c; return true;}
else return false;
}
char victory() { // returns X,O, or '\0'
...
}
};
Most of that could be done with a [3][3] matrix as well, but because cells are private that's an implementation detail and none of anyones buisness. obviously in this case the user would choose a move based on a single number rather than B2 for the center, again implementation detail.
-
LOL, I am sure that is a better way of doing it. Although I do not understand it all that well it looks cool :), but I do like the idea of choosing a spot with only one number instead of the b2. Since I have already coded it with the B2 though, i will keep it for now. I plan on revising this game as this school year goes. Thanks and hopefully by December I will be able to understand your code :) lol
-
Another quick question:
how can i put a 2d array in the prototype for a function?
ex:
void displayBoard(char board);
int main ()
{
board[3][3] = blah blah blah;
displayBoard(board);
}
definition down here
This gives me an error:
error C2664: 'displayBoard' : cannot convert parameter 1 from 'char [10][13]' to 'char'
-
i may be wrong, but try doing it as such...it's been a while since i've done it
void displayBoard(char board[][3]);
i'm even less sure about this one:
void displayBoard(char **board);
{ board[0][1] = 'x'; }
one thing i do know for sure though is you should use toupper() located in <ctype.h>. the way you did it works, but you could have saved yourself some time by using toupper().
toupper() will not change any upper case letters you pass to it;
-
Ok, thanks for the tip on the upper case. I was looking on google for a lil while and said screw it and typed up my own thing. Yeah, the void displayBoard(char board[3][3]); works :). Thanks for the help.
-
no prob.....but for future refence, try it as (char board[][3], int count); and tranverse
like so
Code:
for(i = 0; i < count; i++)
{
for(j = 0; j < 3; j++)
board[i][j];
}
that way you can have any number of board[]...you cannot do it
as board[3][] though....because of pointers and memory referencing which i don't want to get into.