|
Hello all, I am currently a first year B.eng Electrical and Electronic with Information Technolgy as endorsement student.
I am working on a simnple "tictactoe" game... its a simple programme as I have only started programming in C++ very recently. I am struggeling with use the of functions and and the especially using prototypes...Is their anyone who assist me with the programme? any help would be much appreciated....
Below i have the code that was given to me by our lecturer... I have to complete the code. Im sure i can figure out what to do but I would like to better understand how functions work...
//game of tictactoe (noughts and crosses)
#include <iostream>
#include <string>
using namespace std;
//each position on our 3x3 board
// 0 - unused
// 1 - cross
// 2 - nought
int p1x1=0;
int p1x2=0;
int p1x3=0;
int p2x1=0;
int p2x2=0;
int p2x3=0;
int p3x1=0;
int p3x2=0;
int p3x3=0;
//The board is represented by
// | |
// p1x1 | p1x2 | p1x3
// | |
// ---------------------
// | |
// p2x1 | p2x2 | p2x3
// | |
// ---------------------
// | |
// p3x1 | p3x2 | p3x3
// | |
//
//prototype of place_move method used so that we can
//call methods which are yet to be defined.
//INSERT PROTOTYPE HERE
//make a move, false if it cannot be made
bool makemove(int row, int col, int who) {
//INSERT CODE HERE-> WHEN CAN A MOVE NOT BE MADE?
if ((row==1)&&(col==1))
return place_move(p1x1, who);
if ((row==1)&&(col==2))
return place_move(p1x2, who);
if ((row==1)&&(col==3))
return place_move(p1x3, who);
if ((row==2)&&(col==1))
return place_move(p2x1, who);
if ((row==2)&&(col==2))
return place_move(p2x2, who);
if ((row==2)&&(col==3))
return place_move(p2x3, who);
//INSERT CODE HERE
return false;
}
//place a nought or cross if the move can be made
bool place_move(int &location, int who) {
if (location==0) {
//INSERT CODE HERE->LOOK AT RETURN TYPE OF FUNCTION
} else {
//INSERT CODE HERE
}
}
//maps the integer representation of our 0's and X's to 0's and X's we can output on the screen
string piece(int who) {
if (who==1)
return "X";
else if (who==2)
return "O";
else return " ";
}
void draw_board() {
//INSERT CODE TO DRAW THE BOARD
}
//determine if there is a winner by seeing if all the pieces match
int check3(int p1, int p2, int p3) {
//INSERT CODE HERE->LOOK AT RETURN TYPE OF FUNCTION
return 0;
}
bool draw() {
//INSERT CODE HERE IF THE GAME ENDS IN A DRAW
}
//returns 0 if no winner
// 1 if cross wins
// 2 if nought wins
// 3 if the game is a draw
int winner() {
int result;
//rows
result=check3(p1x1, p1x2, p1x3);
if (result!=0) return result;
result=check3(p2x1, p2x2, p2x3);
if (result!=0) return result;
result=check3(p3x1, p3x2, p3x3);
if (result!=0) return result;
//INSERT CODE HERE
//draw: let's return code 3
if (draw()) return 3;
return 0;
}
//let a player take a turn
void play(int player) {
int row;
int col;
int win;
win=winner();
//check for a winner
if ((win!=0)&&(win!=3)) {
cout << "The winner is "<<endl;
cout << " "<< piece(win) << endl;
return;
}
if (draw()==true) {
cout << "The game is a draw "<<endl;
return;
}
if (player==1) {
cout << "It is X's turn to move" <<endl;
cout << "Enter row (1 to 3):" <<endl;
cin >> row;
cout << "Enter col (1 to 3):" <<endl;
cin >> col;
if (makemove(row, col, player)) {
draw_board();
play(2);
} else {
cout << "Invalid move, try again" << endl;
//INSERT CODE HERE->WHAT MUST HAPPEN IF THERE WAS AN INVALID MOVE?
}
} else {
cout << "It is O's turn to move" <<endl;
//INSERT CODE HERE
if /*INSERT CODE HERE*/{
} else {
cout << "Invalid move, try again" << endl;
//INSERT CODE HERE
}
}
}
int main() {
cout << "Welcome to the game of tictactoe " << endl;
draw_board();
play(1);
return 0;
system ("PAUSE");
}
|
|
|
Hi!
please i want to tell you,that the programm you wrote in the article is some problems, such, place_move is not declared.
so please i want this programm, so rewrite it correctly.
thank you..
moowliidyare
|
|
|
Hi!
please i want to tell you,that the programm you wrote in the article is some problems, such, place_move is not declared.
so please i want this programm, so rewrite it correctly.
thank you..
moowliidyare
|
|
|
|
|
|
|
|