Skip to content

Instantly share code, notes, and snippets.

@perforb
Last active October 9, 2015 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save perforb/3480075 to your computer and use it in GitHub Desktop.
Save perforb/3480075 to your computer and use it in GitHub Desktop.
C++初心者が荷物運びゲームをつくってみた
/*
平山尚『ゲームプログラマになる前に覚えておきたい技術』より
お題:荷物運びゲームを2時間以内につくる
*/
#include<iostream>
using namespace std;
char screen[5][8] = {
{'#', '#', '#', '#', '#', '#', '#', '#',},
{'#', ' ', '.', '.', ' ', 'p', ' ', '#',},
{'#', ' ', 'o', 'o', ' ', ' ', ' ', '#',},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', '#',},
{'#', '#', '#', '#', '#', '#', '#', '#',},
};
char input;
int x, y;
bool cleared = false;
void getInput() {
cin >> input;
}
void setPosition() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 8; j++) {
if (screen[i][j] == 'p' || screen[i][j] == 'P') {
x = j;
y = i;
}
}
}
}
bool isGoalPoint(int rx, int ry) {
if ((ry == 1 && rx == 2) || (ry == 1 && rx == 3)) {
return true;
}
return false;
}
void setCleared() {
if (screen[1][2] == 'O' && screen[1][3] == 'O') {
cleared = true;
}
}
void move(int rx, int ry, char c) {
if (c == '#') {
return;
}
else if (c == ' ') {
screen[ry][rx] = 'p';
if (screen[y][x] == 'P') {
screen[y][x] = '.';
}
else {
screen[y][x] = ' ';
}
}
else if (c == '.') {
screen[ry][rx] = 'P';
if (screen[y][x] == 'P') {
screen[y][x] = '.';
}
else {
screen[y][x] = ' ';
}
}
else if (c == 'o' || c == 'O') {
int rxx, ryy;
if (input == 'a') {
rxx = rx - 1;
ryy = ry;
}
else if (input == 's') {
rxx = rx + 1;
ryy = ry;
}
else if (input == 'w') {
rxx = rx;
ryy = ry - 1;
}
else if (input == 'z') {
rxx = rx;
ryy = ry + 1;
}
if (screen[ryy][rxx] == '#' || screen[ryy][rxx] == 'o') {
return;
}
else if (screen[ryy][rxx] == ' ') {
screen[ryy][rxx] = 'o';
if (isGoalPoint(rx, ry)) {
screen[ry][rx] = 'P';
if (screen[y][x] == 'P') {
screen[y][x] = '.';
}
else {
screen[y][x] = ' ';
}
}
else {
screen[ry][rx] = 'p';
if (screen[y][x] == 'P') {
screen[y][x] = '.';
}
else {
screen[y][x] = ' ';
}
}
}
else if (screen[ryy][rxx] == '.') {
screen[ryy][rxx] = 'O';
if (isGoalPoint(rx, ry)) {
screen[ry][rx] = 'P';
if (screen[y][x] == 'P') {
screen[y][x] = '.';
}
else {
screen[y][x] = ' ';
}
}
else {
screen[ry][rx] = 'p';
if (screen[y][x] == 'P') {
screen[y][x] = '.';
}
else {
screen[y][x] = ' ';
}
}
}
else if (screen[ryy][rxx] == ' ') {
screen[ryy][rxx] = 'o';
}
}
}
void updateGame() {
int rx, ry = 0;
char c;
setPosition();
if (input == 'a') {
rx = x - 1;
ry = y;
c = screen[ry][rx];
move(rx, ry, c);
}
else if (input == 's') {
rx = x + 1;
ry = y;
c = screen[ry][rx];
move(rx, ry, c);
}
else if (input == 'w') {
rx = x;
ry = y - 1;
c = screen[ry][rx];
move(rx, ry, c);
}
else if (input == 'z') {
rx = x;
ry = y + 1;
c = screen[ry][rx];
move(rx, ry, c);
}
else {
return;
}
setCleared();
}
void draw() {
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 8; j++) {
cout << screen[i][j];
}
cout << endl;
}
if (cleared) {
cout << "Congratulation's! you won." << endl;
}
else {
cout << "a:left s:right w:up z:down. command?" << endl;
}
}
int main() {
draw();
while (true) {
if (cleared) continue;
getInput();
updateGame();
draw();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment