Connecting Tech Pros Worldwide Help | Site Map

tic tac toe in c++

houserocks13's Avatar
Newbie
 
Join Date: Nov 2006
Posts: 5
#1: Dec 7 '06
Hi, my prof is driving us crazy.. Please help.. there are two errors in this program and i cant figure out what it means..

error C4716: 'randompoint' : must return a value
error C4716: 'addup' : must return a value

Here's the code:

#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>


int array[3][3]={{49, 50, 51},{52,53,54},{55,56,57}};
int player;


void display()
{
int loop, loop2;
system("clear");
for(loop=0;loop<3;loop++)
{
for(loop2=0;loop2<3;loop2++)
{
cout<<" ";
cout<<char(array[loop][loop2]);
}
cout<<"\n";
}
}


int randompoint()
{
int random, random2;
if(array[1][1]!=79 && array[1][1]!=88)
{array[1][1]=79;}
else
{
do{
random=(rand()%3);
random2=(rand()%3);
}while(array[random][random2]==79 || array[random][random2]==88);
array[random][random2]=79;
}
}


int addup(int loop, int total[])
{
int loop3, loop2;
loop3=3; //set to 3 so can check diagonal
for(loop2=0;loop2<3;loop2++)
{
total[0]=array[loop][loop2]+total[0];
total[1]=array[loop2][loop]+total[1];
if(loop==2) //put in so only checks diagonals once
{
loop3--;
total[3]=array[loop2][loop3]+total[3];
total[2]=array[loop2][loop2]+total[2];
}
}
}


int winfun(int whowon)
{
if(whowon==264)
{
display();
cout<<"YOU HAVE WON!\n";
player=1;
}
if(whowon==237)
{
display();
cout<<"THE COMPUTER HAS WON!\n";
player=2;
}
return 1; //tell program that someone won
}


int main()
{
//variables
ofstream outfile;
ifstream infile;
int human=0;
int computer=0;
int noone=0;
float percent_wins, all;
int flag, move, allowed, side, playagain, count;
int loop, loop2, loop3, loop4, loop5, point;
int win=0;
int total[4]={0,0,0,0};
int moves=0;
//Main do loop
do{
do{

display();

allowed=0;
do {
cout<<"Pick a point: ";
cin>>point;
//Initialize the point
for(loop=0;loop<3;loop++)
{
for(loop2=0;loop2<3;loop2++)
{
if(array[loop][loop2]==(point+48) && array[loop][loop2]<=57)
{
array[loop][loop2]=88;
allowed=1;
}
}
}
}while(allowed==0);
moves++;
if(moves<=8) {

move=0;
for(loop=0;loop<3;loop++)
{
addup(loop, total);
//checks totals to see if only 2 X's in it.
for(loop3=0;loop3<4;loop3++)
{
side=79;
do{
if(total[loop3]>(side*2+48) && total[loop3]<(side*2+58) && move!=1)
{
for(loop4=0;loop4<3;loop4++)
{
for(loop5=0;loop5<3;loop5++)
{
if(array[loop4][loop5]==(total[loop3]-(side*2)) && array[loop4][loop5]<=57)
{
array[loop4][loop5]=79;
move=1;
break;
}
}
}
}
side+=9;
}while(side<89);
total[loop3]=0;
}
}
//Random points
if(move!=1)
{
randompoint();
}
}

for(loop=0;loop<3;loop++)
{
addup(loop, total); //adds up for total
//checks to see if worthy of going through win function
for(loop3=0;loop3<4;loop3++)
{
if(total[loop3]==(79*3) || total[loop3]==(88*3))
{win=winfun(total[loop3]);}
total[loop3]=0;
}
}
moves++;
}while(win!=1 && moves<9);
if(win!=1)
{
display();
cout<<"NO ONE WINS!\n";
player=3;
}



outfile.open("SCORES.DAT",ios::app);
outfile<<player<<'\n';
outfile.close();


infile.open("SCORES.DAT",ios::in);
while(!infile.eof())
{
infile>>player;
if(player==1)
human++;
if(player==2)
computer++;
if(player==3)
noone++;
}
infile.close();
cout<<"You have won: "<<human<<" times.\n";
cout<<"The computer won: "<<computer<<" times.\n";
cout<<"Ties: "<<noone<<"\n";
all=computer+human+noone;
percent_wins=(human/all)*100;
cout<<"You have won "<<percent_wins<<" percent of the time.\n";

//End opening files------------------------------

//reset the board-----------------------------------
count=49;
human=0;
computer=0;
noone=0;
for(loop=0;loop<3;loop++)
{
for(loop2=0;loop2<3;loop2++)
{
array[loop][loop2]=count;
count++;
}
}
moves=0;
win=0;
player=0;
cout<<"Do you want to play again? 1=Yes 2=No ";
cin>>playagain;
}while(playagain!=2);
return 0;
}
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,161
#2: Dec 7 '06

re: tic tac toe in c++


Expand|Select|Wrap|Line Numbers
  1. int randompoint()
  2. {
  3.     ...
  4. }
  5.  
You have declared randompoint as returning an int but it doesn't return anything, you should either return a value

Expand|Select|Wrap|Line Numbers
  1. int randompoint()
  2. {
  3.     ...
  4.     return 0;
  5. }
  6.  
or re-declare it as not returning a value

Expand|Select|Wrap|Line Numbers
  1. void randompoint()
  2. {
  3.     ...
  4. }
  5.  
similarly for the other function
Reply