Click here to Skip to main content
16,007,163 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionPassing a 2 dimension array created with new Pin
fleetmanager27-Jan-06 4:32
fleetmanager27-Jan-06 4:32 
QuestionRe: Passing a 2 dimension array created with new Pin
David Crow27-Jan-06 9:45
David Crow27-Jan-06 9:45 
QuestionAuto Enable Menu for dialog. Pin
includeh1027-Jan-06 4:02
includeh1027-Jan-06 4:02 
AnswerRe: Auto Enable Menu for dialog. Pin
Maximilien27-Jan-06 4:56
Maximilien27-Jan-06 4:56 
QuestionYour comments for monitoring users Pin
includeh1027-Jan-06 4:00
includeh1027-Jan-06 4:00 
AnswerRe: Your comments for monitoring users Pin
David Crow27-Jan-06 4:36
David Crow27-Jan-06 4:36 
AnswerRe: Your comments for monitoring users Pin
Maximilien27-Jan-06 7:12
Maximilien27-Jan-06 7:12 
QuestionProblem in moving the hole in a puzzle game Pin
eivanlo27-Jan-06 3:53
eivanlo27-Jan-06 3:53 
#include <stdafx.h>
#include <stdlib.h> //contains random number generator rand()
#include <iostream.h>
#include <process.h> //contains system() and exit syntax
#include <time.h> //contains time seed for random generation
//==========================================================================================
// Function Prototype and global variables
void StartPage(); //Introduction of the game.
bool Playgame(int option); //Ask player to play game or not.
void Reset(char* &p_table); //Reset and generate the puzzle.
void Random(int& holeidx); //Random the puzzle.
void Drawtable(char *p_table); //Draw and display the puzzle.
void Move(int& holeidx,int move); //Moving the hole.
int Correct(int a); //Checking and comparing the puzzle with the answer

int size=0; //The size of the puzzle.
char Answer[26]; //The Answer array.
char* pAns = Answer; //The pointer of the Answer array.
char Temp[26]; //The temporary array to store the playing puzzle
char* pTemp = Temp; //The pointer of the temporary array
int holeidx; //The index number of the "hole" in the array
//==========================================================================================
//Main Program.
void main(){

StartPage(); //Display the instruction to player
Playgame(1); //For 1, ask the player to start game or not

do{ //Control the main flow of this game.
int input; //The input of player movement
int count=0; //The counter of number of moves.

do{ //Restrict the player to select the puzzle size between 2-5
cout << "\nPlese enter the puzzle size! (2-5):\t";
cin >> size;
if(size<2)
cout << "How can you play without any puzzle? Please re-enter";
if(size>5)
cout << "Sorry! It is too large to play! Please re-enter";
}while(size<2 || size >5);

Reset(pAns); //Generate the Answer array with selected size
Reset(pTemp); //Generate the temporary array of playing puzzle with selected size
Random(holeidx); //Generate the temporary array in random order
system ("cls");

do{ //Display the main screen of the game and movement.
system("cls");
cout << "Selected puzzle sizes: " << size;
cout << "\n\nThe target answer is:\n\n\n";

Drawtable(pAns); //Draw and display the Answer puzzle

cout << "\n\nPlease rearrange the following puzzle into correct order!\n\n\n";

Drawtable(pTemp); //Draw and display the playing puzzle

cout << "\n\n" << count++ << " moves so far.\n"; //After each movement, the do while loop will run once.
//Thus we can use count++ to count the number of moves.
Correct(1); //Check how many correct puzzle
cout << " correct puzzles so far <";
Correct(0); //Check and display the corrected letters.
cout << " >\n";

cout << "Which direction to move? (1-9) Press \"q\" to quit: ";

if(Correct(2)!=1){ //If the player still not win the game,
cin.ignore(255,'\n'); //ask and input the way to move the hole.
input = cin.get(); //else if the player win the game, end game
Move(holeidx,input);
}
else{
cout << "\n\nCongratulation! You WIN the game in " << count-1 << " moves.\n\n\a";
break;
}
}while(input!=113);
}while(Playgame(2)!=false); //For 2, Ask to play the game again or not.
}
//==========================================================================================
// StartPage Function:
void StartPage(){
//Show the instructions here
}
//==========================================================================================
//Play Game Function
bool Playgame(int option){
int reply;
return reply;
}
//==========================================================================================
// Reset and generate the puzzle.

void Reset(char* &p_table){

for(int i=0;i<(size*size);i++){
*(p_table + i) = 65 +i; //Generate the array by using ACSII code with selected size
*(p_table +(size*size)-1)=32; //Replace the last element with the hole by using the Space (with ACSII code 32)
}
}
//=========================================================================================
//Ramdom the puzzle

void Random(int& holeidx){

srand(time(0));
int index=0; //The array index random by rand()
char temp; //A temporary variable for swapping

for(int x=0; x<(size*size); x++){ //Randomly pick up an index number of the Temp array.
index = rand()% (size*size); //When x =0, swap the random element with the first element
temp = *(pTemp + x); //When x =1, swap the random element with the second element
*(pTemp + x) = *(pTemp+index); //....etc.
*(pTemp+index) = temp;
}
for(int y=0;y<(size*size);y++){
if(*(pTemp+y)==32) //Locate the index number of the hole.
holeidx=y;
}
}
//=========================================================================================
//Draw and display the Table

void Drawtable(char *p_table){
int r=0; //r = row of the table
int c=0; //c = column of the table
for(r=0;r< size;r++){
for(c=0;c< size;c++)
cout << " | " << *(p_table +(r*size+c)); //Calculate the location of the
cout << " | " << "\n"; //element in a size*size table
}
}
//=========================================================================================
//Moving the hole.

void Move(int& holeidx,int move){

int tar_row; //The row of the target element
int tar_col; //The column of the target element
int hrow; //The row of the hole
int hcol; //The column of the hole
int temp=0; //A temporary variable for swapping
int number; //The index number of the target element

hrow = holeidx / size; //Calculate the row number of the hole
hcol = holeidx % size; //Calculate the column number of the hole

switch(move){

case 49: //(Press 1)
break;
case 50: //(Press 2)
break;
case 51: //(Press 3)
break;
case 52: //(Press 4)
break;
case 53: //(Press 5)
break;
case 54: //(Press 6)
break;
case 55: //(Press 7)
break;
case 56: //(Press 8)
break;
case 57: //(Press 9)
break;
case 113: //(Press q)
case 81: //(Press Q)
exit(1); //If Press Q to quit, exit the game directly.
break;
default:
break;
}


//Check if moving the hole outside the puzzle or not.
//If not, calculate the index of the target element.
//Swapping the hole and the target element.

//If moving outside the puzzle,
//display warning
}
//=========================================================================================
//Checking and comparing the puzzle with the answer

int Correct(int a){

int count=0; //count the number of correct letters
int target=0; //check if the player win the game or not.

return target;
}
AnswerRe: Problem in moving the hole in a puzzle game Pin
David Crow27-Jan-06 4:34
David Crow27-Jan-06 4:34 
QuestionCompiles fine, then doesn't... Pin
the_augy27-Jan-06 2:41
the_augy27-Jan-06 2:41 
AnswerRe: Compiles fine, then doesn't... Pin
Rage27-Jan-06 2:59
professionalRage27-Jan-06 2:59 
GeneralRe: Compiles fine, then doesn't... Pin
the_augy29-Jan-06 23:54
the_augy29-Jan-06 23:54 
QuestionRe: Compiles fine, then doesn't... Pin
David Crow30-Jan-06 2:52
David Crow30-Jan-06 2:52 
QuestionDrag and Drop Pin
Anil_vvs27-Jan-06 1:19
Anil_vvs27-Jan-06 1:19 
QuestionTooltip popup on pointing to text. As a plugin in VS .NET Pin
AlnKG27-Jan-06 0:58
AlnKG27-Jan-06 0:58 
AnswerRe: Tooltip popup on pointing to text. As a plugin in VS .NET Pin
Aravind Badrinath Krishnan27-Jan-06 23:28
Aravind Badrinath Krishnan27-Jan-06 23:28 
QuestionShow or Hide image Pin
waxie27-Jan-06 0:42
waxie27-Jan-06 0:42 
AnswerRe: Show or Hide image Pin
Owner drawn27-Jan-06 0:47
Owner drawn27-Jan-06 0:47 
AnswerRe: Show or Hide image Pin
David Crow27-Jan-06 3:00
David Crow27-Jan-06 3:00 
GeneralRe: Show or Hide image Pin
waxie29-Jan-06 15:49
waxie29-Jan-06 15:49 
Questiontough nut Pin
Great A'Tuin26-Jan-06 22:49
Great A'Tuin26-Jan-06 22:49 
AnswerRe: tough nut Pin
PJ Arends26-Jan-06 23:41
professionalPJ Arends26-Jan-06 23:41 
GeneralRe: tough nut Pin
Great A'Tuin27-Jan-06 0:05
Great A'Tuin27-Jan-06 0:05 
GeneralRe: tough nut Pin
PJ Arends27-Jan-06 0:29
professionalPJ Arends27-Jan-06 0:29 
AnswerRe: easy nut Pin
Owner drawn26-Jan-06 23:57
Owner drawn26-Jan-06 23:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.