Jan 22, 2012 at 8:29pm UTC
I haven't started the word jumble yet, But I'm getting errors in my tic tac toe and menu part. I need help finding and fixing the errors in this, I attempted to compile and fix errors, but I can't fix it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
#include "stdafx.h"
#include<iostream>
#include<string>
#include<ctype.h>
#include <stdlib.h>
#include<cstdlib>
#include <time.h>
const int NUM_ROWS=3;
const int NUM_COLS=3;
const char initVal = ' ' ;
char board [NUM_ROWS][NUM_COLS];
using namespace std;
void Menu(void );
void WordJumble(void );
void TicTacToe(void );
void InitBoard(void );
void Print(void );
void PlayerMove(int );
char Check(int , int , int );
main()
{
Menu();
system("PAUSE" );
return (0);
}
void Menu(void )
{
int gamechoice;
//Ask the user what game they want to play
cout <<"What game do you want to play?\n" ;
cout <<"\t- Click 1 for Tic-Tac-Toe (2 Player Game)\n" ;
cout <<"\t- Click 2 for Word Jumble\n" ;
cin >> gamechoice;
//Run the chosen game
switch (gamechoice)
{
case (1):
TicTacToe();
break ;
case (2):
WordJumble();
break ;
default :
cout <<"Please Pick a Game" ;
break ;
}
}
void TicTacToe(void );
{
int counter;
int p1 =0;
int p2 =0;
int tie = 0;
char winner_check = ' ' ;
char play = 'Y' ;
while (play == 'Y' )
{
InitBoard();
cout << "Tic Tac Toe!" <<endl;
Print();
counter = 0;
do
{
cout << "Player 1: Enter the X, Y coordinates of your move: " ;
PlayerMove(1);
counter++;
winner_check = Check(counter,p1,p2);
if (winner_check != ' ' )
break ;
cout << "Player 2: Enter the X, Y coordinates of your move: " ;
PlayerMove(2);
winner_check = Check(counter,p1,p2);
counter++;
}while (winner_check==' ' && counter <9);
if (winner_check == 'X' )
{
cout << "Player 1 Wins " << endl;
p1++;
}
else if (winner_check == 'Y' )
{
cout << "Player 2 Wins " << endl;
p2++;
}
else
{
cout << "Tie Breaker" << endl;
tie++;
}
cout << "===============================" << endl;
cout << "SCORE " << endl;
cout << "===============================" << endl;
cout << "Player 1:" << p1<< " Player 2:" << p2 << " Tie:" << tie << endl;
cout << "===============================" << endl;
cout << endl;
cout << "Play another game? Y/N " ;
cin >> play;
}
cout << "===============================" << endl;
cout << "FINAL SCORE " << endl;
cout << "===============================" << endl;
cout << "Player 1:" << p1<< " Player 2:" << p2 << " Tie:" << tie << endl;
cout << "===============================" << endl;
cout << endl;
return 0;
}
void InitBoard(void )
{
for (int row =0; row<NUM_ROWS; row++)
for (int col=0;col<NUM_COLS;col++)
board[row][col]=initVal;
}
void Print(void )
{
cout << "=======" << endl;
cout << "|" <<board[0][0] << "|" << board[0][1] << "|" << board[0][2] << "|" << endl;
cout << "=======" << endl;
cout << "|" << board[1][0] <<"|" <<board[1][1]<<"|" <<board[1][2]<<"|" << endl;
cout << "=======" << endl;
cout << "|" << board[2][0] <<"|" <<board[2][1]<<"|" <<board[2][2]<<"|" <<endl;
cout << "=======" << endl << endl;
}
void PlayerMove(int player)
{
int x, y;
int p = player;
char comma;
cin >> x >> comma >> y;
cout << endl;
x--; y--;
if (board[x][y]==initVal)
{
if (player ==1)
board[x][y]='X' ;
else
board[x][y]='O' ;
}
else
{
cout << "Illegal move! Re-Enter the X, Y coordiates of your move: " ;
PlayerMove(p);
}
Print();
}
char Check(int count, int p1, int p2)
{
//Check rows
for (int row = 0; row < NUM_ROWS; row++)
{
if (board[row][0]==board[row][1] && board[row][0]==board[row][2])
return board[row][0];
}
//Check columns
for (int col = 0; col < NUM_COLS; col++)
{
if (board[0][col]==board[1][col] && board[0][col]==board[2][col])
return board[0][col];
}
//Check Diagonals
if (board[0][0] == board[1][1] && board [0][0] == board[2][2])
return board[0][0];
if (board[2][0] == board[1][1] && board [2][0] == board[0][2])
return board[2][0];
return ' ' ;
}
If you could maybe redo the part and send it back, any help would be much appreciated!
Edit1:Changed it to source code.
Last edited on Jan 23, 2012 at 12:01am UTC
Jan 22, 2012 at 9:37pm UTC
Please us code tags, this is unreadable.
Jan 22, 2012 at 9:56pm UTC
@Periphery
Line 52: Do you see anything extra there at the end?
Line 112: You might want to look at the corrected line 52 for a hint.
EDIT: I originally said lines 51 and 111. My mistake; I had to delete the "stdafx.h" header to compile the program, shifting everything up by one line.
-Albatross
Last edited on Jan 23, 2012 at 3:34pm UTC
Jan 22, 2012 at 11:59pm UTC
I'm new to C++, I don't really see the thing I added at the end at line 51, as well as line 111.
Last edited on Jan 23, 2012 at 12:05am UTC
Jan 23, 2012 at 10:05am UTC
I think Albatross means lines 52 and 112.