help with matching card game!

Implemented using functions only. (NO LOOPS AND ARRAYS)

Page 1 of 1

2 Replies - 1456 Views - Last Post: 09 April 2008 - 11:56 AM Rate Topic: -----

#1 Yaheda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-April 08

help with matching card game!

Post icon  Posted 09 April 2008 - 03:20 AM

The game is to be implemented using functions only.(NO LOOP CONSTuRCT OR ARRAYS)
Here is the problem;

The memory game is a game played with 10 cards, each placed face down. Each card has a capital letter printed on it. There are exactly 5 matching pairs of cards.
Each turn is as follows:

The player turns two cards upright.
If the cards are the same, then the cards remain upright.
If the cards are different, then the cards are turned face down again.
The cards always remain in the same relative position. Write a C++ program to implement this game. The game stops when all cards have been matched.
Once the game has concluded, ask the user if he/she wishes to play again.
Hint: You decide what the cards will be!
Hint: you will need to remember the letter on the card, as well as whether the card is face down or not.

I'm realy confused and this assignment is due this friday.

Here is what i've done so far:

#include <iostream>
#include <string>

using namespace std;

//each position placed on a 5x2 board
// 0 - unused
// 1 - face up
// 2 - face down

int p1x1=0;
int p1x2=0;
int p1x3=0;
int p1x4=0;
int p1x5=0;

int p2x1=0;
int p2x2=0;
int p2x3=0;
int p2x4=0;
int p2x5=0;

//here is the board
//	  |	 |	 |	 |
// p1x1 |p1x2 |p1x3 |p1x4 |p1x5
//	  |	 |	 |	 |
// -----------------------------
//	  |	 |	 |	 |
// p2x1 |p2x2 |p2x3 |p2x4 |p2x5
//	  |	 |	 |	 |
//

//Prototype of card_select method used so that we can
//call methods wc r yet to be defined.

bool card_select(int &location, int who);

//make a move, false if it can't be made

bool make_move(int row, int col, int who) {
	 if ((row<1)||(row>2)) return false;
	 if ((col<1)||(col>5)) return false;
	 
	 if ((row==1)&&(col==1))
		return card_select(p1x1,who);
	 if ((row==1)&&(col==2))
		return card_select(p1x2,who);
	 if ((row==1)&&(col==3))
		return card_select(p1x3,who);
	 if ((row==1)&&(col==4))
		return card_select(p1x4,who);
	 if ((row==1)&&(col==5))
		return card_select(p1x5,who);

	 if ((row==2)&&(col==1))
		return card_select(p2x1,who);
	 if ((row==2)&&(col==2))
		return card_select(p2x2,who);
	 if ((row==2)&&(col==3))
		return card_select(p2x3,who);
	 if ((row==2)&&(col==4))
		return card_select(p2x4,who);
	 if ((row==2)&&(col==5))
		return card_select(p2x5,who);
		
	 return false;
}

bool card_select(int &location, int who) {
	 if (location==0) {
					  location=who;
					  return true;
	 } else {
			return false;
			}
}

//map the integers representing the letters
string peice(int who) {
	   if (who=11)
	   return "A1";
	   else if (who=12)
	   return "A2";
	   
	   else if (who=21)
	   return "B1";
	   else if (who=22)
	   return "B2";
	   
	   else if (who=31)
	   return "C1";
	   else if (who=32)
	   return "C2";
	   
	   else if (who=41)
	   return "D1";
	   else if (who=42)
	   return "D2";
	   
	   else if (who=51)
	   return "E1";
	   else if (who=52)
	   return "E2";
	   
	   else return " ";
}






void draw_board() {
	 cout << " " << peice(p1x1) << " | " << peice(p1x2) << " | " << peice(p1x3) << " | " << peice(p1x4) << " | " << peice(p1x5) << " " << endl;
	 cout << "-----------------------" << endl;
	 cout << " " << peice(p2x1) << " | " << peice(p2x2) << " | " << peice(p2x3) << " | " << peice(p2x4) << " | " << peice(p2x5) << " " << endl;
}

//determine if the cards match
int check(int p1, int p2) {
	if (p1==p2)
	return p1;
	return 0;
}

bool draw() {
	 return ((p1x1!=0)&&(p1x2!=0)&&(p1x3!=0)&&(p1x4!=0)&&(p1x5!=0)&&
			 (p2x1!=0)&&(p2x2!=0)&&(p2x3!=0)&&(p2x4!=0)&&(p2x5!=0));
}

//if 1 - face up
//if 2 - face down


int winner() {
	int result;
	
	result=check(p1x1, p1x2);
	if (result!=0) return result;
	result=check(p1x1, p1x3);
	if (result!=0) return result;
	result=check(p1x1, p1x4);
	if (result!=0) return result;
	result=check(p1x1, p1x5);
	if (result!=0) return result;
				 
	result=check(p1x1, p2x1);
	if (result!=0) return result;
	result=check(p1x1, p2x2);
	if (result!=0) return result;
	result=check(p1x1, p2x3);
	if (result!=0) return result;
	result=check(p1x1, p2x4);
	if (result!=0) return result;
	result=check(p1x1, p2x5);
	if (result!=0) return result;
	
	if (draw()) return 3;
	return 0;
	
}


void play(int player) {
	 int row;
	 int col;
	 int win;
	 
	 win=winner();
	 
	 if (player==1) {
					cout << "Enter row (1 or 2):" << endl;
					cin >> row;
					cout << "Enter column (1 to 5):" << endl;
					cin >> col;
					
					if (make_move(row, col, player)) {
									   draw_board();
									   play(1);
					} else {		   
									   cout << "Invalid move, try again" << endl;
									   play(1);
									   }
					}
}					
int main() {
	cout << "Welcome to the matching card game " << endl;
	draw_board();
	play(1);
	return 0;
}



Please, can anyone unblock my situation, it would be realy helpfull....

*Mod Edit: added code tags: :code:

This post has been edited by NickDMax: 09 April 2008 - 01:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: help with matching card game!

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: help with matching card game!

Posted 09 April 2008 - 04:10 AM

put code in [*code] [*/code] tags without the * for readability please
Was This Post Helpful? 0
  • +
  • -

#3 Yaheda  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-April 08

Re: help with matching card game!

Posted 09 April 2008 - 11:56 AM

I'm really sorry but I don't understand what u mean by
""put code in [*code] [*/code] tags without the *"""
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1