adding cs142 code
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
CPPFLAGS=-Wall -g -std=c++0x
|
||||
|
||||
all: cootie
|
||||
|
||||
cootie: cootie.cc dice.o player.o
|
||||
|
||||
dice.o: dice.cc dice.h
|
||||
player.o: player.cc player.h
|
||||
|
||||
test: all
|
||||
./cootie 4
|
||||
|
||||
clean:
|
||||
@rm -rvf cootie cootie.dSYM *.o
|
||||
@@ -0,0 +1,42 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cstdlib>
|
||||
#include "dice.h"
|
||||
#include "player.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
const string usage = "usage: cootie <number of players>";
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
if(argc < 2) {
|
||||
cerr << usage << endl;;
|
||||
return 1;
|
||||
}
|
||||
|
||||
random_init();
|
||||
|
||||
int number_of_players = int(atof(argv[1]));
|
||||
vector<player> players;
|
||||
for(int i=0; i < number_of_players; i++) {
|
||||
players.push_back(player(i));
|
||||
}
|
||||
|
||||
auto keep_playing = true;
|
||||
while(keep_playing) {
|
||||
for(player & p : players) {
|
||||
// cout << p << endl;
|
||||
p.turn();
|
||||
if(p.won()) {
|
||||
keep_playing = false;
|
||||
cout << "player " << p.id << " wins!!" << endl;
|
||||
cout << p << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
cout << "total rolls: " << get_roll_count() << endl;
|
||||
cout << "thanks for playing" << endl;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include <cstdlib>
|
||||
#include <ctime>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "dice.h"
|
||||
|
||||
static int roll_count = 0;
|
||||
|
||||
void random_init() {
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
pid_t pid = getpid();
|
||||
srand(tv.tv_usec + pid);
|
||||
}
|
||||
|
||||
int roll() {
|
||||
roll_count++;
|
||||
int compliment = rand() % 6;
|
||||
return compliment + 1;
|
||||
}
|
||||
|
||||
int get_roll_count() {
|
||||
return roll_count;
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#ifndef __DICE_H__
|
||||
#define __DICE_H__
|
||||
|
||||
void random_init();
|
||||
int roll();
|
||||
int get_roll_count();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,67 @@
|
||||
#include "dice.h"
|
||||
#include "player.h"
|
||||
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
player::player(int id) : id(id), head(false), body(false),
|
||||
hat(false), eyes(false), mouth(false), legs(0) {}
|
||||
|
||||
ostream & operator<<(ostream & os, const player & e) {
|
||||
os << "{id: " << e.id
|
||||
<< ", body: " << e.body
|
||||
<< ", head: " << e.head
|
||||
<< ", hat: " << e.hat
|
||||
<< ", eyes: " << e.eyes
|
||||
<< ", mouth: " << e.mouth
|
||||
<< ", legs: " << e.legs
|
||||
<< "}";
|
||||
return os;
|
||||
}
|
||||
|
||||
void player::turn() {
|
||||
bool turn_finished = false;
|
||||
while(not turn_finished) {
|
||||
int cur_roll = roll();
|
||||
turn_finished = true;
|
||||
if(cur_roll == 1) {
|
||||
if(not body) {
|
||||
turn_finished = false;
|
||||
body = true;
|
||||
}
|
||||
}
|
||||
else if(body and cur_roll == 2) {
|
||||
if(not head) {
|
||||
turn_finished = false;
|
||||
head = true;
|
||||
}
|
||||
}
|
||||
else if(body and head) {
|
||||
if (cur_roll == 3) {
|
||||
turn_finished = false;
|
||||
hat = true;
|
||||
}
|
||||
else if (cur_roll == 4) {
|
||||
turn_finished = false;
|
||||
eyes = true;
|
||||
}
|
||||
else if (cur_roll == 5) {
|
||||
turn_finished = false;
|
||||
mouth = true;
|
||||
}
|
||||
else if (cur_roll == 6) {
|
||||
if (not (legs >= 6)) {
|
||||
turn_finished = false;
|
||||
legs++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool player::won() {
|
||||
if(head and body and hat and eyes and mouth and legs == 6)
|
||||
return true;
|
||||
else
|
||||
return false;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef __PLAYER_H__
|
||||
#define __PLAYER_H__
|
||||
|
||||
#include <ostream>
|
||||
using namespace std;
|
||||
|
||||
class player {
|
||||
public:
|
||||
int id;
|
||||
bool head;
|
||||
bool body;
|
||||
bool hat;
|
||||
bool eyes;
|
||||
bool mouth;
|
||||
int legs;
|
||||
|
||||
player(int);
|
||||
friend ostream & operator<<(ostream &, const player &);
|
||||
|
||||
void turn();
|
||||
bool won();
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user