I'm making a tick tack toe game for a programming class and i*m stuck with some problems.
Problem 1: rand sometimes returns a value over the max value. Max is 9 and it can return up to 1000000 on some occasions.
Problem 2: The Win function only works in player vs player mode.
As i said, this is for a programming class so don't give me finished code, just give me tips and what i do wrong.
Thanks in advance.
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* main.cc
* Copyright (C) Albin Mattsson 2009 <albin.mattsson@gmail.com>
*
* main.cc is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* main.cc is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
int WinFunc(char Slot[9]){
if(Slot[0] == 88 && Slot[1] == 88 && Slot[2] == 88){return 1;}
else if(Slot[3] == 88 && Slot[4] == 88 && Slot[5] == 88){return 1;}
else if(Slot[6] == 88 && Slot[7] == 88 && Slot[8] == 88){return 1;}
else if(Slot[0] == 88 && Slot[3] == 88 && Slot[6] == 88){return 1;}
else if(Slot[1] == 88 && Slot[4] == 88 && Slot[7] == 88){return 1;}
else if(Slot[2] == 88 && Slot[5] == 88 && Slot[8] == 88){return 1;}
else if(Slot[0] == 88 && Slot[4] == 88 && Slot[8] == 88){return 1;}
else if(Slot[2] == 88 && Slot[4] == 88 && Slot[6] == 88){return 1;}
else if(Slot[0] == 88 && Slot[1] == 88 && Slot[2] == 88){return 2;}
else if(Slot[3] == 88 && Slot[4] == 88 && Slot[5] == 88){return 2;}
else if(Slot[6] == 88 && Slot[7] == 88 && Slot[8] == 88){return 2;}
else if(Slot[0] == 88 && Slot[3] == 88 && Slot[6] == 88){return 2;}
else if(Slot[1] == 88 && Slot[4] == 88 && Slot[7] == 88){return 2;}
else if(Slot[2] == 88 && Slot[5] == 88 && Slot[8] == 88){return 2;}
else if(Slot[0] == 88 && Slot[4] == 88 && Slot[8] == 88){return 2;}
else if(Slot[2] == 88 && Slot[4] == 88 && Slot[6] == 88){return 2;}
else if((Slot[0] > 57 || Slot[0] <48) &&
(Slot[1] > 57 || Slot[1] <48) &&
(Slot[2] > 57 || Slot[2] <48) &&
(Slot[3] > 57 || Slot[3] <48) &&
(Slot[4] > 57 || Slot[4] <48) &&
(Slot[5] > 57 || Slot[5] <48) &&
(Slot[6] > 57 || Slot[6] <48) &&
(Slot[7] > 57 || Slot[7] <48) &&
(Slot[8] > 57 || Slot[8] <48)){return 3;}
else{return 0;}
}
void Play(int b){
int PlayerOne(0), PlayerTwo(0), Win(0), n(0);
char Slot[10];
bool MenuState = true, MenuStateOne(0), MenuStateTwo(0);
for(int n = 0; n < 10; n++){
Slot[n] = 49 + n;}
while(MenuState == true){
MenuStateOne = true;
MenuStateTwo = true;
cout << endl << endl
<< " " << Slot[0] << " | " << Slot[1] << " | " << Slot[2] << endl
<< " ---------" << endl
<< " " << Slot[3] << " | " << Slot[4] << " | " << Slot[5] << endl
<< " ---------" << endl
<< " " << Slot[6] << " | " << Slot[7] << " | " << Slot[8] << endl;
Win = WinFunc(Slot);
if(Win == 2){
cout << endl << "Player two won!" << endl;
cout << endl << endl << endl;
break;}
else if(Win == 3){
cout << endl << "It's a tie!" << endl;
cout << endl << endl << endl;
break;}
cout << Win;
while(MenuStateOne == true){
cout << endl << "Player one, please press the nummber you choose: ";
cin >> PlayerOne;
PlayerOne--;
if(Slot[PlayerOne] == 49 + PlayerOne){Slot[PlayerOne] = 88; MenuStateOne = false;}
else{cout << endl << "Invalid entry, please re enter";}}
if(b == 0){
cout << endl << endl
<< " " << Slot[0] << " | " << Slot[1] << " | " << Slot[2] << endl
<< " ---------" << endl
<< " " << Slot[3] << " | " << Slot[4] << " | " << Slot[5] << endl
<< " ---------" << endl
<< " " << Slot[6] << " | " << Slot[7] << " | " << Slot[8] << endl;}
Win = WinFunc(Slot);
if(Win == 1){
cout << endl << "Player one won!" << endl;
cout << endl << endl << endl;
break;}
else if(Win == 3){
cout << endl << "It's a tie!" << endl;
cout << endl << endl << endl;
break;}
while(MenuStateTwo == true){
if(b == 0){
cout << endl << "Player two, please press the nummber you choose: ";
cin >> PlayerTwo;
PlayerTwo--;
if(Slot[PlayerTwo] == 49 + PlayerTwo){Slot[PlayerTwo] = 79; MenuStateTwo = false;}
else{cout << endl << "Invalid entry, please re enter";}}
else if(b == 1){
PlayerTwo = rand()%9;
cout << PlayerTwo;
if(Slot[PlayerTwo] == 49 + PlayerTwo){Slot[PlayerTwo] = 79; MenuStateTwo = false;}}
else if(b == 2){
PlayerTwo = rand()%9;
if(Slot[0] == 79 && Slot[1] == 79 && Slot[2] == 51){PlayerTwo = 2; MenuStateTwo = false;}
else if(Slot[1] == 79 && Slot[2] == 79 && Slot[0] == 49){PlayerTwo = 0; MenuStateTwo = false;}
else if(Slot[3] == 79 && Slot[4] == 79 && Slot[5] == 54){PlayerTwo = 5; MenuStateTwo = false;}
else if(Slot[4] == 79 && Slot[5] == 79 && Slot[3] == 52){PlayerTwo = 3; MenuStateTwo = false;}
else if(Slot[6] == 79 && Slot[7] == 79 && Slot[8] == 57){PlayerTwo = 9; MenuStateTwo = false;}
else if(Slot[7] == 79 && Slot[8] == 79 && Slot[6] == 55){PlayerTwo = 6; MenuStateTwo = false;}
else if(Slot[0] == 79 && Slot[3] == 79 && Slot[6] == 55){PlayerTwo = 6; MenuStateTwo = false;}
else if(Slot[3] == 79 && Slot[6] == 79 && Slot[0] == 49){PlayerTwo = 0; MenuStateTwo = false;}
else if(Slot[1] == 79 && Slot[4] == 79 && Slot[7] == 56){PlayerTwo = 7; MenuStateTwo = false;}
else if(Slot[4] == 79 && Slot[7] == 79 && Slot[1] == 50){PlayerTwo = 1; MenuStateTwo = false;}
else if(Slot[2] == 79 && Slot[5] == 79 && Slot[8] == 57){PlayerTwo = 8; MenuStateTwo = false;}
else if(Slot[5] == 79 && Slot[8] == 79 && Slot[2] == 51){PlayerTwo = 2; MenuStateTwo = false;}
else if(Slot[0] == 79 && Slot[4] == 79 && Slot[8] == 57){PlayerTwo = 8; MenuStateTwo = false;}
else if(Slot[4] == 79 && Slot[8] == 79 && Slot[0] == 49){PlayerTwo = 0; MenuStateTwo = false;}
else if(Slot[2] == 79 && Slot[4] == 79 && Slot[6] == 55){PlayerTwo = 6; MenuStateTwo = false;}
else if(Slot[4] == 79 && Slot[6] == 79 && Slot[2] == 51){PlayerTwo = 2; MenuStateTwo = false;}
else if(Slot[0] == 79 && Slot[2] == 79 && Slot[1] == 50){PlayerTwo = 1; MenuStateTwo = false;}
else if(Slot[3] == 79 && Slot[5] == 79 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
else if(Slot[6] == 79 && Slot[8] == 79 && Slot[7] == 56){PlayerTwo = 7; MenuStateTwo = false;}
else if(Slot[0] == 79 && Slot[6] == 79 && Slot[3] == 52){PlayerTwo = 3; MenuStateTwo = false;}
else if(Slot[1] == 79 && Slot[7] == 79 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
else if(Slot[2] == 79 && Slot[8] == 79 && Slot[5] == 54){PlayerTwo = 5; MenuStateTwo = false;}
else if(Slot[0] == 79 && Slot[8] == 79 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
else if(Slot[2] == 79 && Slot[6] == 79 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
else if(Slot[0] == 88 && Slot[1] == 88 && Slot[2] == 51){PlayerTwo = 2; MenuStateTwo = false;}
else if(Slot[1] == 88 && Slot[2] == 88 && Slot[0] == 49){PlayerTwo = 0; MenuStateTwo = false;}
else if(Slot[3] == 88 && Slot[4] == 88 && Slot[5] == 54){PlayerTwo = 5; MenuStateTwo = false;}
else if(Slot[4] == 88 && Slot[5] == 88 && Slot[3] == 52){PlayerTwo = 3; MenuStateTwo = false;}
else if(Slot[6] == 88 && Slot[7] == 88 && Slot[8] == 57){PlayerTwo = 9; MenuStateTwo = false;}
else if(Slot[7] == 88 && Slot[8] == 88 && Slot[6] == 55){PlayerTwo = 6; MenuStateTwo = false;}
else if(Slot[0] == 88 && Slot[3] == 88 && Slot[6] == 55){PlayerTwo = 6; MenuStateTwo = false;}
else if(Slot[3] == 88 && Slot[6] == 88 && Slot[0] == 49){PlayerTwo = 0; MenuStateTwo = false;}
else if(Slot[1] == 88 && Slot[4] == 88 && Slot[7] == 56){PlayerTwo = 7; MenuStateTwo = false;}
else if(Slot[4] == 88 && Slot[7] == 88 && Slot[1] == 50){PlayerTwo = 1; MenuStateTwo = false;}
else if(Slot[2] == 88 && Slot[5] == 88 && Slot[8] == 57){PlayerTwo = 8; MenuStateTwo = false;}
else if(Slot[5] == 88 && Slot[8] == 88 && Slot[2] == 51){PlayerTwo = 2; MenuStateTwo = false;}
else if(Slot[0] == 88 && Slot[4] == 88 && Slot[8] == 57){PlayerTwo = 8; MenuStateTwo = false;}
else if(Slot[4] == 88 && Slot[8] == 88 && Slot[0] == 49){PlayerTwo = 0; MenuStateTwo = false;}
else if(Slot[2] == 88 && Slot[4] == 88 && Slot[6] == 55){PlayerTwo = 6; MenuStateTwo = false;}
else if(Slot[4] == 88 && Slot[6] == 88 && Slot[2] == 51){PlayerTwo = 2; MenuStateTwo = false;}
else if(Slot[0] == 88 && Slot[2] == 88 && Slot[1] == 50){PlayerTwo = 1; MenuStateTwo = false;}
else if(Slot[3] == 88 && Slot[5] == 88 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
else if(Slot[6] == 88 && Slot[8] == 88 && Slot[7] == 56){PlayerTwo = 7; MenuStateTwo = false;}
else if(Slot[0] == 88 && Slot[6] == 88 && Slot[3] == 52){PlayerTwo = 3; MenuStateTwo = false;}
else if(Slot[1] == 88 && Slot[7] == 88 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
else if(Slot[2] == 88 && Slot[8] == 88 && Slot[5] == 54){PlayerTwo = 5; MenuStateTwo = false;}
else if(Slot[0] == 88 && Slot[8] == 88 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
else if(Slot[2] == 88 && Slot[6] == 88 && Slot[4] == 53){PlayerTwo = 4; MenuStateTwo = false;}
if(Slot[PlayerTwo] == 49 + PlayerTwo){Slot[PlayerTwo] = 79; MenuStateTwo = false;}}
}
}
}
int main(){
bool MenuState = true;
string Choice;
srand((unsigned)time(0));
while(MenuState == true){
cout << "Welcome to Albin's tick tack toe game!" << endl
<< "Play vs [p]layer" << endl
<< "Player vs [n]ormal computer" << endl
<< "Player vs [h]ard computer" << endl
<< "[E]xit" << endl;
cin >> Choice;
if(Choice == "p" || Choice == "P"){Play(0);}
else if(Choice == "n" || Choice == "N"){Play(1);}
else if(Choice == "h" || Choice == "H"){Play(2);}
else if(Choice == "e" || Choice == "E"){MenuState = false;}
cout << endl << endl << endl;}
}