SlideShare a Scribd company logo
Mouse and Cat Game In C++
Mouse and Cat game in C++
This is a mouse and cat game
controlled by specific characters to
play the game. Their positions are
controlled randomly throughout the
game.
#include "MouseAndCatGame.h"
MouseAndCatGame::MouseAndCatGame(Inter
face* pui, Player* pplayer)
:underground_(), p_ui(pui),
p_player(pplayer), cheatModeOn(false)
{
//prepare game
//mouse state already set up in
its contructor
//set up cat
cat_.spotMouse(&mouse_);
postionAtRandom();
}
const RandomNumberGenerator
MouseAndCatGame::rng_;
void MouseAndCatGame::run() {
p_ui-
>drawGridOnScreen(prepareGrid());
key_ = p_ui-
>getKeyPressFromUser();
const int KEY_C = 67;
while (!hasEnded(key_))
{
if (isArrowKeyCode(key_))
{
mouse_.scamper(key_);
if (!cheatModeOn)
cat_.chaseMouse();
p_ui-
>drawGridOnScreen(prepareGrid());
applyRules();
}
else if (key_ == KEY_C)
{
// toggle the cheat
mode
cheatModeOn =
!cheatModeOn;
p_ui-
>drawGridOnScreen(prepareGrid());
}
key_ = p_ui-
>getKeyPressFromUser();
}
p_ui-
>showResultsOnScreen(prepareEndMessage
());
}
string MouseAndCatGame::prepareGrid()
const{
//prepare a string that holds
the grid information
ostringstream os;
for (int row(1); row <= SIZE;
++row) //for each row (vertically)
{
for (int col(1); col <=
SIZE; ++col) //for each column
(horizontally)
{
if ((row == cat_.y_)
&& (col == cat_.x_))
os <<
cat_.getSymbol(); //show cat
else
if ((row ==
mouse_.getY()) && (col ==
mouse_.getX()))
os <<
mouse_.getSymbol(); //show mouse
else
if
((nut_.hasBeenCollected() == false) &&
(row == nut_.getY()) && (col ==
nut_.getX()))
os <<
nut_.getSymbol();
else
{
if
(underground_.checkForHole(col, row))
os <<
underground_.getHoleSymbol();
else
os << FREECELL; //show free grid
cell
}
} //end of col-loop
os << endl;
} //end of row-loop
os << endl << p_player-
>getName() << " Score: " <<
p_player->getScoreAmount();
if (nut_.hasBeenCollected())
os <<
collectedNutMessage();
if (cheatModeOn)
os << "nCheat Mode
Activated!";
return os.str();
} //end prepareGrid
bool
MouseAndCatGame::isArrowKeyCode(const
int keycode) const{
return (keycode == LEFT) ||
(keycode == RIGHT) || (keycode == UP)
|| (keycode == DOWN);
}
void MouseAndCatGame::applyRules(){
if (cat_.hasCaughtMouse())
{
mouse_.die();
p_player-
>updateScoreAmount(-1);
}
else if (mouse_.hasNut(&nut_) &&
mouse_.hasReachedAHole(&underground_))
{
mouse_.escapeIntoHole();
p_player-
>updateScoreAmount(1);
}
}
bool MouseAndCatGame::hasEnded(char
key) const{
return ((key == QUIT) ||
(!mouse_.isAlive()) ||
(mouse_.hasEscaped()));
}
string
MouseAndCatGame::collectedNutMessage()
const{
return "nTHE NUT HAS BEEN
COLLECTED";
}
string
MouseAndCatGame::prepareEndMessage()
const{
ostringstream os;
if (mouse_.hasEscaped())
os << "nnEND OF GAME: THE
MOUSE ESCAPED UNDERGROUND!";
else
if (!mouse_.isAlive())
os << "nnEND OF
GAME: THE CAT ATE THE MOUSE!";
else
os << "nnEND OF
GAME: THE PLAYER ENDED THE GAME!";
return os.str();
}
void
MouseAndCatGame::postionAtRandom(){
int x = 0;
int y = 0;
do
{
x =
rng_.getRandomValue(SIZE);
y =
rng_.getRandomValue(SIZE);
nut_.setPosition(x, y);
} while
(underground_.checkForHole(x, y));
do
{
x =
rng_.getRandomValue(SIZE);
y =
rng_.getRandomValue(SIZE);
mouse_.setPosition(x, y);
} while
((underground_.checkForHole(x, y)) ||
(nut_.isAtPosition(x, y)));
do
{
x =
rng_.getRandomValue(SIZE);
y =
rng_.getRandomValue(SIZE);
cat_.setPosition(x, y);
} while
((underground_.checkForHole(x, y)) ||
(nut_.isAtPosition(x, y)) ||
(mouse_.isAtPosition(x, y)));
}
ostream&
MouseAndCatGame::toFile(ostream& os)
const{
os << setfill('0')
<< setw(2) << mouse_.getX()
<< "/"
<< setw(2) << mouse_.getY()
<< "/"
<< setw(2) << cat_.getX()
<< "/"
<< setw(2) << cat_.getY()
<< "/"
<< setw(2) << nut_.getX()
<< "/"
<< setw(2) << nut_.getY()
<< "/"
<< setw(2) <<
nut_.hasBeenCollected() << "/";
return os;
}
istream&
MouseAndCatGame::fromFile(istream&
is){
char ch;
is >> mouse_.x_ >> ch
>> mouse_.y_ >> ch
>> cat_.x_ >> ch
>> cat_.y_ >> ch
>> nut_.x_ >> ch
>> nut_.y_ >> ch
>> nut_.collected_ >>ch;
return is;
}
ostream& operator<<(ostream& os, const
MouseAndCatGame& mcg){
return (mcg.toFile(os));
}
istream& operator>>(istream& is,
MouseAndCatGame& mcg){
return (mcg.fromFile(is));
}
Mouse and Cat Game In C++

More Related Content

DOCX
Create xo game in android studio
MahmoodGhaemMaghami
 
PDF
Flappy bird
SantiagoYepesSerna
 
PDF
The Ring programming language version 1.10 book - Part 71 of 212
Mahmoud Samir Fayed
 
PDF
Python Developer's Daily Routine
Maxim Avanov
 
PDF
Basics
Logan Campbell
 
PDF
RandomGuessingGame
Christopher Singleton
 
Create xo game in android studio
MahmoodGhaemMaghami
 
Flappy bird
SantiagoYepesSerna
 
The Ring programming language version 1.10 book - Part 71 of 212
Mahmoud Samir Fayed
 
Python Developer's Daily Routine
Maxim Avanov
 
RandomGuessingGame
Christopher Singleton
 

What's hot (20)

PPT
Linux mouse
sean chen
 
PDF
201707 CSE110 Lecture 13
Javier Gonzalez-Sanchez
 
PDF
The Ring programming language version 1.5.3 book - Part 79 of 184
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.6 book - Part 61 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.2 book - Part 50 of 84
Mahmoud Samir Fayed
 
PPTX
TicketBEKA? Ticket booking
Likhith Pujari
 
KEY
20110424 action scriptを使わないflash勉強会
Hiroki Mizuno
 
PDF
The Ring programming language version 1.3 book - Part 42 of 88
Mahmoud Samir Fayed
 
PDF
PyCon2009_AI_Alt
Hiroshi Ono
 
PDF
The Ring programming language version 1.5.2 book - Part 50 of 181
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.4.1 book - Part 15 of 31
Mahmoud Samir Fayed
 
DOC
เกมจับคู่1
JAy YourJust'one
 
PDF
The Ring programming language version 1.6 book - Part 53 of 189
Mahmoud Samir Fayed
 
PDF
The Ring programming language version 1.8 book - Part 57 of 202
Mahmoud Samir Fayed
 
PDF
Python Data-Science-Preview
whuang022ai
 
PDF
2c astable monostable
yeksdech
 
PDF
The Ring programming language version 1.5.1 book - Part 49 of 180
Mahmoud Samir Fayed
 
PPTX
Project 2
umtkrc
 
PDF
The Ring programming language version 1.5.3 book - Part 62 of 184
Mahmoud Samir Fayed
 
PDF
Fizzbuzzalooza
Kevlin Henney
 
Linux mouse
sean chen
 
201707 CSE110 Lecture 13
Javier Gonzalez-Sanchez
 
The Ring programming language version 1.5.3 book - Part 79 of 184
Mahmoud Samir Fayed
 
The Ring programming language version 1.6 book - Part 61 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.2 book - Part 50 of 84
Mahmoud Samir Fayed
 
TicketBEKA? Ticket booking
Likhith Pujari
 
20110424 action scriptを使わないflash勉強会
Hiroki Mizuno
 
The Ring programming language version 1.3 book - Part 42 of 88
Mahmoud Samir Fayed
 
PyCon2009_AI_Alt
Hiroshi Ono
 
The Ring programming language version 1.5.2 book - Part 50 of 181
Mahmoud Samir Fayed
 
The Ring programming language version 1.4.1 book - Part 15 of 31
Mahmoud Samir Fayed
 
เกมจับคู่1
JAy YourJust'one
 
The Ring programming language version 1.6 book - Part 53 of 189
Mahmoud Samir Fayed
 
The Ring programming language version 1.8 book - Part 57 of 202
Mahmoud Samir Fayed
 
Python Data-Science-Preview
whuang022ai
 
2c astable monostable
yeksdech
 
The Ring programming language version 1.5.1 book - Part 49 of 180
Mahmoud Samir Fayed
 
Project 2
umtkrc
 
The Ring programming language version 1.5.3 book - Part 62 of 184
Mahmoud Samir Fayed
 
Fizzbuzzalooza
Kevlin Henney
 
Ad

Viewers also liked (8)

PPTX
Jess Adaptation Hunger Games
colesmedia
 
PPTX
Enrichment Next Steps - PowerPoint 2
colesmedia
 
PPT
Games C L
gail.dyer
 
PDF
Word games in c
Programming Homework Help
 
DOC
Basic version of MS Paint created using Turbo C++
Jaison Sabu
 
PPTX
1 c – graphic designs
Haseeb Patel
 
PPT
C ppt
jasmeen kr
 
PPSX
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
Jess Adaptation Hunger Games
colesmedia
 
Enrichment Next Steps - PowerPoint 2
colesmedia
 
Games C L
gail.dyer
 
Word games in c
Programming Homework Help
 
Basic version of MS Paint created using Turbo C++
Jaison Sabu
 
1 c – graphic designs
Haseeb Patel
 
C ppt
jasmeen kr
 
INTRODUCTION TO C PROGRAMMING
Abhishek Dwivedi
 
Ad

Similar to Mouse and Cat Game In C++ (20)

PDF
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 
PDF
package com.tictactoe; public class Main {public void play() {.pdf
info430661
 
PDF
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
PDF
public interface Game Note interface in place of class { .pdf
kavithaarp
 
PDF
import java.util.Scanner;public class Main {    public static in.pdf
anwarsadath111
 
PPT
Mobile Game and Application with J2ME
Jenchoke Tachagomain
 
PPT
Mobile Game and Application with J2ME - Collision Detection
Jenchoke Tachagomain
 
PDF
The Ring programming language version 1.3 book - Part 52 of 88
Mahmoud Samir Fayed
 
PDF
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
fazilfootsteps
 
PDF
#include stdio.h #include string.h #include stdlib.h #in.pdf
singhanubhav1234
 
PDF
Cocos2dx 7.1-7.2
Kyungryul KIM
 
PDF
Hello!This is Java assignment applet.Can someone help me writing.pdf
jyothimuppasani1
 
PDF
The Ring programming language version 1.10 book - Part 81 of 212
Mahmoud Samir Fayed
 
PDF
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
PDF
MineSweeper.java public class MS { public static void main(Strin.pdf
aniyathikitchen
 
PPTX
Windows Phone: From Idea to Published Game in 75 minutes
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
TXT
Tgh.pl
iskabom
 
PDF
The Ring programming language version 1.5.2 book - Part 66 of 181
Mahmoud Samir Fayed
 
PDF
Html5 game, websocket e arduino
Giuseppe Modarelli
 
PPTX
Students to Business Day 2012: Rob Miles
Frederik De Bruyne
 
ObjectiveCreate a graphical game of minesweeper IN JAVA. The boar.pdf
rajkumarm401
 
package com.tictactoe; public class Main {public void play() {.pdf
info430661
 
The main class of the tictoe game looks like.public class Main {.pdf
asif1401
 
public interface Game Note interface in place of class { .pdf
kavithaarp
 
import java.util.Scanner;public class Main {    public static in.pdf
anwarsadath111
 
Mobile Game and Application with J2ME
Jenchoke Tachagomain
 
Mobile Game and Application with J2ME - Collision Detection
Jenchoke Tachagomain
 
The Ring programming language version 1.3 book - Part 52 of 88
Mahmoud Samir Fayed
 
Ifgqueue.h#ifndef LFGQUEUE_H #define LFGQUEUE_H#include pl.pdf
fazilfootsteps
 
#include stdio.h #include string.h #include stdlib.h #in.pdf
singhanubhav1234
 
Cocos2dx 7.1-7.2
Kyungryul KIM
 
Hello!This is Java assignment applet.Can someone help me writing.pdf
jyothimuppasani1
 
The Ring programming language version 1.10 book - Part 81 of 212
Mahmoud Samir Fayed
 
In Java using Eclipse, Im suppose to write a class that encapsulat.pdf
anjandavid
 
MineSweeper.java public class MS { public static void main(Strin.pdf
aniyathikitchen
 
Windows Phone: From Idea to Published Game in 75 minutes
Microsoft Developer Network (MSDN) - Belgium and Luxembourg
 
Tgh.pl
iskabom
 
The Ring programming language version 1.5.2 book - Part 66 of 181
Mahmoud Samir Fayed
 
Html5 game, websocket e arduino
Giuseppe Modarelli
 
Students to Business Day 2012: Rob Miles
Frederik De Bruyne
 

More from Programming Homework Help (7)

PDF
Java Assignment Help
Programming Homework Help
 
PDF
C# Assignmet Help
Programming Homework Help
 
PDF
C# Assignmet Help
Programming Homework Help
 
PDF
Family tree in java
Programming Homework Help
 
PDF
Binary tree in java
Programming Homework Help
 
PDF
Bank account in java
Programming Homework Help
 
PDF
Card Games in C++
Programming Homework Help
 
Java Assignment Help
Programming Homework Help
 
C# Assignmet Help
Programming Homework Help
 
C# Assignmet Help
Programming Homework Help
 
Family tree in java
Programming Homework Help
 
Binary tree in java
Programming Homework Help
 
Bank account in java
Programming Homework Help
 
Card Games in C++
Programming Homework Help
 

Recently uploaded (20)

PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PPTX
CDH. pptx
AneetaSharma15
 
PPTX
Basics and rules of probability with real-life uses
ravatkaran694
 
PPTX
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
PPTX
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
PPTX
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
PPTX
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
PPTX
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
PPTX
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
PPTX
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Virus sequence retrieval from NCBI database
yamunaK13
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Gupta Art & Architecture Temple and Sculptures.pptx
Virag Sontakke
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
CDH. pptx
AneetaSharma15
 
Basics and rules of probability with real-life uses
ravatkaran694
 
INTESTINALPARASITES OR WORM INFESTATIONS.pptx
PRADEEP ABOTHU
 
Continental Accounting in Odoo 18 - Odoo Slides
Celine George
 
Artificial-Intelligence-in-Drug-Discovery by R D Jawarkar.pptx
Rahul Jawarkar
 
Kanban Cards _ Mass Action in Odoo 18.2 - Odoo Slides
Celine George
 
Sonnet 130_ My Mistress’ Eyes Are Nothing Like the Sun By William Shakespear...
DhatriParmar
 
HEALTH CARE DELIVERY SYSTEM - UNIT 2 - GNM 3RD YEAR.pptx
Priyanshu Anand
 
BASICS IN COMPUTER APPLICATIONS - UNIT I
suganthim28
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 

Mouse and Cat Game In C++

  • 2. Mouse and Cat game in C++ This is a mouse and cat game controlled by specific characters to play the game. Their positions are controlled randomly throughout the game. #include "MouseAndCatGame.h" MouseAndCatGame::MouseAndCatGame(Inter face* pui, Player* pplayer) :underground_(), p_ui(pui), p_player(pplayer), cheatModeOn(false) { //prepare game //mouse state already set up in its contructor //set up cat cat_.spotMouse(&mouse_); postionAtRandom(); } const RandomNumberGenerator MouseAndCatGame::rng_; void MouseAndCatGame::run() { p_ui- >drawGridOnScreen(prepareGrid()); key_ = p_ui- >getKeyPressFromUser();
  • 3. const int KEY_C = 67; while (!hasEnded(key_)) { if (isArrowKeyCode(key_)) { mouse_.scamper(key_); if (!cheatModeOn) cat_.chaseMouse(); p_ui- >drawGridOnScreen(prepareGrid()); applyRules(); } else if (key_ == KEY_C) { // toggle the cheat mode cheatModeOn = !cheatModeOn; p_ui- >drawGridOnScreen(prepareGrid()); } key_ = p_ui- >getKeyPressFromUser(); } p_ui- >showResultsOnScreen(prepareEndMessage ()); } string MouseAndCatGame::prepareGrid() const{ //prepare a string that holds the grid information ostringstream os; for (int row(1); row <= SIZE; ++row) //for each row (vertically) {
  • 4. for (int col(1); col <= SIZE; ++col) //for each column (horizontally) { if ((row == cat_.y_) && (col == cat_.x_)) os << cat_.getSymbol(); //show cat else if ((row == mouse_.getY()) && (col == mouse_.getX())) os << mouse_.getSymbol(); //show mouse else if ((nut_.hasBeenCollected() == false) && (row == nut_.getY()) && (col == nut_.getX())) os << nut_.getSymbol(); else { if (underground_.checkForHole(col, row)) os << underground_.getHoleSymbol(); else os << FREECELL; //show free grid cell } } //end of col-loop os << endl; } //end of row-loop
  • 5. os << endl << p_player- >getName() << " Score: " << p_player->getScoreAmount(); if (nut_.hasBeenCollected()) os << collectedNutMessage(); if (cheatModeOn) os << "nCheat Mode Activated!"; return os.str(); } //end prepareGrid bool MouseAndCatGame::isArrowKeyCode(const int keycode) const{ return (keycode == LEFT) || (keycode == RIGHT) || (keycode == UP) || (keycode == DOWN); } void MouseAndCatGame::applyRules(){ if (cat_.hasCaughtMouse()) { mouse_.die(); p_player- >updateScoreAmount(-1); } else if (mouse_.hasNut(&nut_) && mouse_.hasReachedAHole(&underground_)) { mouse_.escapeIntoHole(); p_player- >updateScoreAmount(1); } } bool MouseAndCatGame::hasEnded(char key) const{
  • 6. return ((key == QUIT) || (!mouse_.isAlive()) || (mouse_.hasEscaped())); } string MouseAndCatGame::collectedNutMessage() const{ return "nTHE NUT HAS BEEN COLLECTED"; } string MouseAndCatGame::prepareEndMessage() const{ ostringstream os; if (mouse_.hasEscaped()) os << "nnEND OF GAME: THE MOUSE ESCAPED UNDERGROUND!"; else if (!mouse_.isAlive()) os << "nnEND OF GAME: THE CAT ATE THE MOUSE!"; else os << "nnEND OF GAME: THE PLAYER ENDED THE GAME!"; return os.str(); } void MouseAndCatGame::postionAtRandom(){ int x = 0; int y = 0; do { x = rng_.getRandomValue(SIZE);
  • 7. y = rng_.getRandomValue(SIZE); nut_.setPosition(x, y); } while (underground_.checkForHole(x, y)); do { x = rng_.getRandomValue(SIZE); y = rng_.getRandomValue(SIZE); mouse_.setPosition(x, y); } while ((underground_.checkForHole(x, y)) || (nut_.isAtPosition(x, y))); do { x = rng_.getRandomValue(SIZE); y = rng_.getRandomValue(SIZE); cat_.setPosition(x, y); } while ((underground_.checkForHole(x, y)) || (nut_.isAtPosition(x, y)) || (mouse_.isAtPosition(x, y))); } ostream& MouseAndCatGame::toFile(ostream& os) const{ os << setfill('0') << setw(2) << mouse_.getX() << "/"
  • 8. << setw(2) << mouse_.getY() << "/" << setw(2) << cat_.getX() << "/" << setw(2) << cat_.getY() << "/" << setw(2) << nut_.getX() << "/" << setw(2) << nut_.getY() << "/" << setw(2) << nut_.hasBeenCollected() << "/"; return os; } istream& MouseAndCatGame::fromFile(istream& is){ char ch; is >> mouse_.x_ >> ch >> mouse_.y_ >> ch >> cat_.x_ >> ch >> cat_.y_ >> ch >> nut_.x_ >> ch >> nut_.y_ >> ch >> nut_.collected_ >>ch; return is; } ostream& operator<<(ostream& os, const MouseAndCatGame& mcg){ return (mcg.toFile(os)); } istream& operator>>(istream& is, MouseAndCatGame& mcg){ return (mcg.fromFile(is)); }