Tell me what i did wrong!
It does run, but i would like some feedback on how i could have made it better; For those who aren't sure the values you see repeated, 88 and 79, are ASCII X and O respectively.
I'm not so interested in portability issues, more functionality issues that would make it smoother overall!
Remember, this is my real program in 3 years!
Code:
#include <cstdlib>
#include <iostream>
#include <windows.h>
#include <stdio.h>
#include <conio.h>
using namespace std;
void gotoxy(int, int); // Function that places cursor for DOS
const void drawborder(); // Draws the border around the edge
const void spalsh(); // Draws the 'splash screen'
inline const int whoFirst();
void whoWon(int);
enum player {ONE, TWO};
class Board //The class for the board
{
public:
Board() ;
~Board() {} ;
void drawBoard() const;
int compMove(); //the computers move!
bool checkBoard(); //check if the game is won!
void makeMove(int move, int player); // make the move player
bool testMove(int move) const;
void testBoard() { theBoard[1][1] = 88;}; //dec. 88 is ASCII X
bool isWon() { return (won); };
private:
int theBoard[3][3];
bool won;
};
Board::Board(){ // Constructor, initalize theBoard with the asii number 49 - 57 for 1-9
int in=49;
for(int x=0; x<3; x++){
for(int y=0;y<3;y++){
theBoard[y][x] = in++;
}
}
won = false;
}
int Board::compMove() {
if(theBoard[1][0] == 79 && theBoard[2][0]==79 && theBoard[0][0]==49 || theBoard[0][1]==79 && theBoard[0][2]==79 && theBoard[0][0]==49 || theBoard[0][0]==49 && theBoard[1][1] == 79 && theBoard[2][2]==79){
return 1;
}
if(theBoard[0][0] == 79 && theBoard[2][0]==79 && theBoard[1][0]==50 || theBoard[1][1]==79 && theBoard[1][2]==79 && theBoard[1][0]==50) {
return 2;
}
if(theBoard[1][0] == 79 && theBoard[2][0]==51 && theBoard[0][0]==79 || theBoard[2][2]==79 && theBoard[2][0]==51 && theBoard[2][1]==79 || theBoard[2][0]==51 && theBoard[1][1] == 79 && theBoard[0][2]==79){
return 3;
}
if(theBoard[0][0] == 79 && theBoard[0][1]==52 && theBoard[0][2]==79 || theBoard[0][1]==52 && theBoard[1][1]==79 && theBoard[2][1]==79) {
return 4;
}
if(theBoard[0][1] == 79 && theBoard[1][1]==53 && theBoard[2][1]==79 || theBoard[1][0]==79 && theBoard[1][1]==53 && theBoard[1][2]==79) {
return 5;
}
if(theBoard[0][1] == 79 && theBoard[1][1]==79 && theBoard[2][1]==54 || theBoard[2][0]==79 && theBoard[2][2]==79 && theBoard[2][1]==54) {
return 6;
}
if(theBoard[0][1] == 79 && theBoard[0][0]==79 && theBoard[0][2]==55 || theBoard[1][2]==79 && theBoard[2][2]==79 && theBoard[0][2]==55 || theBoard[0][2]==55 && theBoard[1][1] == 79 && theBoard[2][0]==79){
return 7;
}
if(theBoard[1][1] == 79 && theBoard[1][0]==79 && theBoard[1][2]==56 || theBoard[0][2]==79 && theBoard[1][2]==56 && theBoard[2][2]==79) {
return 8;
}
if(theBoard[0][2] == 79 && theBoard[1][2]==79 && theBoard[2][2]==57 || theBoard[2][1]==79 && theBoard[2][2]==57 && theBoard[2][0]==79 || theBoard[2][2]==57 && theBoard[1][1] == 79 && theBoard[0][0]==79){
return 9;
}
if(theBoard[1][0] == 88 && theBoard[2][0]==88 && theBoard[0][0]==49 || theBoard[0][1]==88 && theBoard[0][2]==88 && theBoard[0][0]==49 || theBoard[0][0]==49 && theBoard[1][1] == 88 && theBoard[2][2]==88){
return 1;
}
if(theBoard[0][0] == 88 && theBoard[2][0]==88 && theBoard[1][0]==50 || theBoard[1][1]==88 && theBoard[1][2]==88 && theBoard[1][0]==50) {
return 2;
}
if(theBoard[1][0] == 88 && theBoard[2][0]==51 && theBoard[0][0]==88 || theBoard[2][2]==88 && theBoard[2][0]==51 && theBoard[2][1]==88 || theBoard[2][0]==51 && theBoard[1][1] == 88 && theBoard[0][2]==88){
return 3;
}
if(theBoard[0][0] == 88 && theBoard[0][1]==52 && theBoard[0][2]==88 || theBoard[0][1]==52 && theBoard[1][1]==88 && theBoard[2][1]==88) {
return 4;
}
if(theBoard[0][1] == 88 && theBoard[1][1]==53 && theBoard[2][1]==88 || theBoard[1][0]==88 && theBoard[1][1]==53 && theBoard[1][2]==88) {
return 5;
}
if(theBoard[0][1] == 88 && theBoard[1][1]==88 && theBoard[2][1]==54 || theBoard[2][0]==88 && theBoard[2][2]==88 && theBoard[2][1]==54) {
return 6;
}
if(theBoard[0][1] == 88 && theBoard[0][0]==88 && theBoard[0][2]==55 || theBoard[1][2]==88 && theBoard[2][2]==88 && theBoard[0][2]==55 || theBoard[0][2]==55 && theBoard[1][1] == 88 && theBoard[2][0]==88){
return 7;
}
if(theBoard[1][1] == 88 && theBoard[1][0]==88 && theBoard[1][2]==56 || theBoard[0][2]==88 && theBoard[1][2]==56 && theBoard[2][2]==88) {
return 8;
}
if(theBoard[0][2] == 88 && theBoard[1][2]==88 && theBoard[2][2]==57 || theBoard[2][1]==88 && theBoard[2][2]==57 && theBoard[2][0]==88 || theBoard[2][2]==57 && theBoard[1][1] == 88 && theBoard[0][0]==88){
return 9;
}
if(theBoard[1][1]==53){
return 5;
}
if(theBoard[0][0]==88 && theBoard[1][2]==56 && theBoard[1][0]!=88){
return 8;
}
if(theBoard[2][0]==88 && theBoard[1][2]==56 && theBoard[1][0]!=88){
return 8;
}
if(theBoard[0][2]==88 && theBoard[1][0]==50 && theBoard[1][2]!=88){
return 2;
}
if(theBoard[2][2]==88 && theBoard[1][0]==50 && theBoard[1][2]!=88){
return 2;
}
if(theBoard[1][0]==88 && theBoard[2][0]==55 && theBoard[0][2]!=88){
return 3;
}
if(theBoard[2][1]==88 && theBoard[2][2]==57 && theBoard[0][0]!=88){
return 9;
}
if(theBoard[1][2]==88 && theBoard[0][2]==55 && theBoard[2][0]!=88){
return 8;
}
if(theBoard[0][1]==88 && theBoard[0][0]==49 && theBoard[2][2]!=88){
return 1;
}
if(theBoard[1][1]=88){
int ranCho = (rand() % (9 - 1 + 9) + 1);
while (ranCho == 5){
ranCho=(rand() % (9 - 1 + 9) + 1);
(rand() % (9 - 1 + 9) + 1);
}
return ranCho;
}
}
bool Board::checkBoard(){
if((theBoard[0][0] == 88 && theBoard[1][0] ==88 && theBoard[2][0] ==88) || (theBoard[0][0] ==79 && theBoard[1][0]==79 && theBoard[2][0]==79)) {
this->won = true;
return true;
}
else if((theBoard[0][1] == 88 && theBoard[1][1] ==88 && theBoard[2][1] ==88) || (theBoard[0][1] ==79 && theBoard[1][1]==79 && theBoard[2][1]==79)) {
this->won = true;
return true;
}
else if((theBoard[0][2] == 88 && theBoard[1][2] ==88 && theBoard[2][2] ==88) || (theBoard[0][2] ==79 && theBoard[1][2]==79 && theBoard[2][2]==79)) {
this->won = true;
return true;
}
else if((theBoard[0][0] == 88 && theBoard[0][1] ==88 && theBoard[0][2] ==88) || (theBoard[0][0] ==79 && theBoard[0][1]==79 && theBoard[0][2]==79)) {
this->won = true;
return true;
}
else if((theBoard[1][0] == 88 && theBoard[1][1] ==88 && theBoard[1][2] ==88) || (theBoard[1][0] ==79 && theBoard[1][1]==79 && theBoard[1][2]==79)) {
this->won = true;
return true;
}
else if((theBoard[2][0] == 88 && theBoard[2][1] ==88 && theBoard[2][2] ==88) || (theBoard[2][0] ==79 && theBoard[2][1]==79 && theBoard[2][2]==79)) {
this->won = true;
return true;
}
else if((theBoard[0][0] == 88 && theBoard[1][1] ==88 && theBoard[2][2] ==88) || (theBoard[0][0] ==79 && theBoard[1][1]==79 && theBoard[2][2]==79)) {
this->won = true;
return true;
}
else if((theBoard[0][2] == 88 && theBoard[1][1] ==88 && theBoard[2][0] ==88) || (theBoard[0][2] ==79 && theBoard[1][1]==79 && theBoard[2][0]==79)) {
this->won = true;
return true;
}
else {
return false;
}
}
void Board::drawBoard() const {
/* | |
------------
| |
------------
| |
Draw the board*/
gotoxy(50,8);
cout << char(theBoard[0][0]) << " | " << char(theBoard[1][0]) << " | " << char(theBoard[2][0]);
gotoxy(50,9);
cout << "-----------";
gotoxy(50,10);
cout << char(theBoard[0][1]) << " | " << char(theBoard[1][1]) << " | " << char(theBoard[2][1]);
gotoxy(50,11);
cout << "-----------";
gotoxy(50,12);
cout << char(theBoard[0][2]) << " | " << char(theBoard[1][2]) << " | " << char(theBoard[2][2]);
}
void Board::makeMove(int move, int player){
switch (move){
case 1:
if(player==ONE){
theBoard[0][0]=88;
}
else{
theBoard[0][0]=79;
}
break;
case 2:
if(player==ONE){
theBoard[1][0]=88;
}
else{
theBoard[1][0]=79;
}
break;
case 3:
if(player==ONE){
theBoard[2][0]=88;
}
else{
theBoard[2][0]=79;
}
break;
case 4:
if(player==ONE){
theBoard[0][1]=88;
}
else{
theBoard[0][1]=79;
}
break;
case 5:
if(player==ONE){
theBoard[1][1]=88;
}
else{
theBoard[1][1]=79;
}
break;
case 6:
if(player==ONE){
theBoard[2][1]=88;
}
else{
theBoard[2][1]=79;
}
break;
case 7:
if(player==ONE){
theBoard[0][2]=88;
}
else{
theBoard[0][2]=79;
}
break;
case 8:
if(player==ONE){
theBoard[1][2]=88;
}
else{
theBoard[1][2]=79;
}
break;
case 9:
if(player==ONE){
theBoard[2][2]=88;
}
else{
theBoard[2][2]=79;
}
break;
}
this->drawBoard();
}
bool Board::testMove(int move) const{
if (move == 1 && theBoard[0][0]!= 49){
return false;
}
else if(move ==2 && theBoard[1][0]!= 50){
return false;
}
else if(move ==3 && theBoard[2][0]!= 51){
return false;
}
else if(move ==4 && theBoard[0][1]!= 52){
return false;
}
else if(move ==5 && theBoard[1][1]!= 53){
return false;
}
else if(move ==6 && theBoard[2][1]!= 54){
return false;
}
else if(move ==7 && theBoard[0][2]!= 55){
return false;
}
else if(move ==8 && theBoard[1][2]!= 56){
return false;
}
else if(move ==9 && theBoard[2][2]!= 57){
return false;
}
else {
return true;
}
}
const void drawborder(){ // Draw Border around edge
for (int x = 1; x < 79; x++){
gotoxy(x,0);
cout <<"-";
gotoxy(x,23);
cout <<"-";
}
for(int y =0; y <= 23; y++){
gotoxy(0,y);
cout << "|";
gotoxy(79,y);
cout << "|";
}
}
void gotoxy(int x, int y) // Place the cursor in DOS
{
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
const void splash(){
gotoxy(22,10);
cout << "Welcome to Solo TicTacToe!";
int blink=0;
int ch =0;
time_t start_time, cur_time;
int test=0;
do {
switch(blink){
case 0:
gotoxy(22,11);
cout << "press any key to continue";
blink=1;
Sleep(1000);
continue;
case 1:
gotoxy(22,11);
cout << " ";
blink=0;
Sleep(1000);
continue;
}
}while((ch = kbhit()) == 0);
gotoxy(22,11);
cout << " ";
gotoxy(22,10);
cout << " ";
ch=getch();
}
inline const int whoFirst(){
srand(time(NULL));
return (rand() % (1 - 0 + 1) + 0);
}
void whoWon(int player){
gotoxy(4,9);
cout << " ";
gotoxy(4,10);
cout << " ";
gotoxy(4,11);
cout << " ";
if(player==3){
gotoxy(4,10);
cout << "Tie!!!!!!!!!!!";
return;
}
if(player==ONE){
gotoxy(4,10);
cout << "Computer Player Wins!!!!!!";
return;
}
if(player==TWO){
gotoxy(4,11);
cout << "You Win!!!!!!!!!!!!!";
return;
}
}
int main(int argc, char *argv[])
{
drawborder();
splash();
Board TicTac;
TicTac.drawBoard();
int whoTurn = whoFirst(), choice, ch, count=0; // Find out who's going first 0 for player, 1 for comp
while (!(TicTac.isWon())){
if(count==9){
whoTurn=3;
whoWon(whoTurn);
break;
}
if(whoTurn == ONE){
gotoxy(4,9);
cout << " ";
gotoxy(4,9);
cout << "Your Turn!";
gotoxy(4,10);
cout << "What spot would you like (1-9): ";
cin >> choice;
if (choice < 0 || choice >10){ //if the choice is out side 1-9 clear and retry.
gotoxy(4,10);
cout << " ";
continue;
}
if (TicTac.testMove(choice)){ //Pass to testMove and check the move
TicTac.makeMove(choice, whoTurn);
if(TicTac.checkBoard()){
break;
}
whoTurn=TWO;
count++;
continue;
}
else{
gotoxy(4,10);
cout << " ";
continue;
}
}
if(whoTurn == TWO){
gotoxy(4,9);
cout << "Computers Turn!";
gotoxy(4,10);
cout << "What spot should i take... ";
choice = TicTac.compMove();
gotoxy(4,11);
cout << "I choose spot " << choice;
TicTac.makeMove(choice, whoTurn);
whoTurn=ONE;
if(TicTac.checkBoard()){
break;
}
Sleep(2000);
gotoxy(4,11);
cout << " ";
count++;
continue;
}
}
if (whoTurn!=3){
whoWon(whoTurn);
}
gotoxy(2,22);
system("PAUSE");
return EXIT_SUCCESS;
}