hello everyone...
i'm new to c++ programming previously in visual basic but want to write cross platform code. here is simple program i tried to make but some behaviors aren't as i expected...i'm using g++ compiler.
1. How to perform data validation on cin? I've read on the internet about validating numbers and use try catch however if the user enters invalid data the program ends with general protection fault - the input is two characters, how to limit cin to two characters?
2. I created a namespace 'board' but it still wasn't recognized in my routines despite 'using namespace board;' i still had to add board::board[][] for example....
3. In the main routine i used a variable 'i' but it kept getting reset to 0 after cin. I tried renaming the variable but it kept happening so i put it in a different namespace 'board' and it no longer was reset to 0.
Thanks if you can offer any advice.
Code:
/* Simple tictactoe program Human vs Human */
namespace std;
#include<iostream>
namespace board {
char board[3][3]; // contains the board array
int i=0; // I put this here because it was getting resest to 0 after cin
}
/*
Draw tic tac toe board
*/
void board_draw()
{
using namespace board;
cout << "\n";
cout << " A B C \n\n";
for (int x=0;x<3;x++) {
cout << x << " ";
for (int y=0;y<3;y++) {
cout << board::board[x][y];
if (y<2) cout << "|";
}
if (x<2) cout << "\n -----\n";
}
cout << "\n\n";
}
/* Set tic tac toe board to blanks */
void board_initialize()
{
using namespace board;
for (int x=0;x<3;x++)
for (int y=0;y<3;y++)
board::board[x][y]=' ';
}
/*
Set tic tac toe board piece
Ex. board_set("A0",'X');
*/
bool board_set(char c[2],char a)
{
using namespace board;
int x,y;
switch (c[0]) {
case 'a':
case 'A':
y=0;
break;
case 'b':
case 'B':
y=1;
break;
case 'c':
case 'C':
y=2;
break;
}
switch (c[1]) {
case '0':
x=0;
break;
case '1':
x=1;
break;
case '2':
x=2;
break;
}
board::board[x][y]=a;
return true;
}
/* Get board piece at position X,Y */
char board_get(int x, int y)
{
int c;
using namespace board;
c=board::board[x][y];
return c;
}
/* Check for winner return 'X' for X winning or 'Y' for Y winning */
char board_winner()
{
using namespace board;
char c[2]={ 'X','Y' };
char win='\0';
for (int i=0;i<2;i++) {
for (int x=0;x<3;x++) {
if (board::board[x][0]==c[i] && board::board[x][1]==c[i] && board::board[x][2]==c[i])
win=c[i];
for (int y=0;y<3;y++) {
if (board::board[0][y]==c[i] && board::board[1][y]==c[i] && board::board[2][y]==c[i])
win=c[i];
}
}
if (board::board[0][0]==c[i] && board::board[1][1]==c[i] && board::board[2][2]==c[i])
win=c[i];
if (board::board[0][2]==c[i] && board::board[1][1]==c[i] && board::board[2][0]==c[i])
win=c[i];
}
return win;
}
/* main program */
int main()
{
char c[2];
char pieces[2]={ 'X', 'Y' };
int i=0;
board_initialize ();
board_draw ();
while (!board_winner()) {
cout << "Enter your move " << pieces[board::i] << ": ";
try {
std::cin >> c;
board_set (c,pieces[board::i]);
board_draw ();
board::i++;
if (board::i>1) board::i=0;
}
catch (out_of_range) {
std::cin.clear();
cout << "\nInvalid entry! Try again: ";
}
}
cout << "Winner is: " << board_winner();
}