Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download

CSC112 Spring 2016 Examples

2370 views
This is the result of our first bit of development for the minemopper program.
We got as far as making the smiley face go through it's motions.  Here is the
code listing for the MineMopper pieces.  (The entire compilable package can
be downloaded here:

* [[media:MineMopper-part1.tar.gz]]

== Makefile ==
'''File:''' Makefile

<syntaxhighlight lang="makefile">
CPPFLAGS=-g --std=gnu++11
LD=g++
CC=g++

smileyTest: smileyTest.cpp Widget.o SmileyFace.o keystream.o

clean:
	rm -f *.o smileyTest
</syntaxhighlight>


== Widget.h ==
'''File:''' Widget.h
<syntaxhighlight lang="cpp">
#ifndef Widget_h
#define Widget_h

class Widget {

 public:
  Widget(int _x, int _y, int _width, int _height);
  Widget();


  //drawing functions
  virtual void display()=0;
  virtual void clear();

  //set our parameters
  virtual int x();
  virtual void x(int _x);
  virtual int y();
  virtual void y(int _y);
  virtual int width();
  virtual void width(int _width);
  virtual int height();
  virtual void height(int _height);

 protected:
    int _x;
    int _y;
    int _width;
    int _height;
};

#endif // Widget_h
</syntaxhighlight>


== Widget.cpp ==
'''File:''' Widget.cpp
#include "Widget.h"
#include "termmanip.h"
#include <iostream>

using namespace std;

Widget::Widget(int _x, int _y, int _width, int _height)
{
  this->_x = _x;
  this->_y = _y;
  this->_width = _width;
  this->_height = _height;
}


Widget::Widget()
{
  this->_x = 0;
  this->_y = 0;
  this->_width = 0;
  this->_height = 0;
}


void
Widget::clear()
{
  //paint over the widget
  for(int i=0; i<_height; i++) {
    cout << cursorPosition(_x, _y+i);
    for(int j=0; j<_width; j++) {
      cout << ' ';
    }
  }

  cout.flush();
}

int
Widget::x()
{
  return _x;
}


void
Widget::x(int _x)
{
  this->_x = _x;
}


int
Widget::y()
{
  return _y;
}


void
Widget::y(int _y)
{
  this->_y = _y;
}


int
Widget::width()
{
  return _width;
}


void
Widget::width(int _width)
{
  this->_width = _width;
}


int
Widget::height()
{
  return _height;
}


void
Widget::height(int _height)
{
  this->_height = _height;
}
</syntaxhighlight>


== SmileyFace.h ==
'''File:''' SmileyFace.h
<syntaxhighlight lang="cpp">
#ifndef SmileyFace_h
#define SmileyFace_h

#include "Widget.h"


class GameGrid;

class SmileyFace : public Widget {

 public:
  SmileyFace(int _x, int _y);

  virtual void scare();
  virtual void chill();
  virtual void kill();
  virtual void click();
  virtual void display();
  virtual void reset();
  
 private:
  
  enum {SCARED, CHILL, DEAD, NEUTRAL} state;
};

#endif // SmileyFace_h
</syntaxhighlight>

== SmileyFace.cpp ==
'''File: ''' SmileFace.cpp
<syntaxhighlight lang="cpp">
#include <unistd.h>
#include <iostream>
#include "termmanip.h"
#include "SmileyFace.h"

using namespace std;


SmileyFace::SmileyFace(int _x, int _y) : Widget(_x, _y, 5, 1)
{
  state = NEUTRAL;
}


void SmileyFace::scare()
{
  //stay scared for a short time
  state = SCARED;
  display();
  usleep(500000);
  state = NEUTRAL;
  display();
}


void SmileyFace::chill()
{
  state = CHILL;
  display();
}


void SmileyFace::kill()
{
  state = DEAD;
  display();
}


void SmileyFace::click()
{

}


void SmileyFace::display()
{
  cout << cursorPosition(_x, _y);
  switch(state) {
  case NEUTRAL:
    cout << "(^_^)";
    break;
    
  case CHILL:
    cout << "($_$)";
    break;
    
  case SCARED:
    cout << "(O_O)";
    break;
    
  case DEAD:
    cout << "(x_x)";
    break;
  }
  cout.flush();
}


void SmileyFace::reset()
{
  state = NEUTRAL;
  display();
}
</syntaxhighlight>


== smileyTest.cpp ==
'''File:''' smileyTest.cpp

<syntaxhighlight lang="cpp">
#include <unistd.h>
#include "termmanip.h"
#include "SmileyFace.h"
#include "keystream.h"

using namespace std;


int main()
{
  SmileyFace *face = new SmileyFace(42, 10);  //put the smiley near the middle!
  keycode c;

  kin.rawMode();

  //clear the screen and display the menu
  cout << clearScreen << cursorOff;
  cout << cursorPosition(1,1)
       << "Q - Scare   C - Chill    Z - Kill   UP - Reset   ESC - Exit";
  cout.flush();

  //draw the initial smiley
  face->display();

  do {
    c = kin.getKey();
    switch(c) {
    case 'q':
    case 'Q':
      face->scare();
      break;

    case 'c':
    case 'C':
      face->chill();
      break;

    case 'z':
    case 'Z':
      face->kill();
      break;

    case UP:
      face->reset();
      break;
    }

    face->display();
  } while(c != ESC);

  return 0;
}
</syntaxhighlight>