Skip to content

Instantly share code, notes, and snippets.

@oha-yashi
Last active August 27, 2019 12:48
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 oha-yashi/a04e89335fc1c6b64c009deb2884a40d to your computer and use it in GitHub Desktop.
Save oha-yashi/a04e89335fc1c6b64c009deb2884a40d to your computer and use it in GitHub Desktop.
slot/slot.c
#include <ncurses.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include "define.h"
void flashBox(); /* 演出 */
int checkError(void); /* windowSizeからゲーム開始可能か判定する */
void resetChecker(void); /* チェッカーをSPINに戻す */
void colorSetting(void); /* 色の設定 */
void putReel(int winy, int winx, int rn, int num); /* 座標とリール番号とリール位置から表示を行う */
void putReel_halfAfter(int winy, int winx, int rn, int num); /* リールを半分進める */
void putReel_threeLine(int winy, int winx, int rn, int num); /* 真ん中の段の座標を入れて,三段表示 */
void putReel_threeAfter(int winy, int winx, int rn, int num); /* 半分進めて三段表示 */
void rotate(int difficulty); /* difficulty(illustの使用範囲)に応じて回転表示 */
int difficultyGate(int difficulty, int rn, int num); /* difficulty未満のillustになるまでリールを回す */
int decisionGate(int dicision, int rn, int num); /* dicisionのillustまでリールを滑らせる */
void drawBox(int line); /* 背景枠の描画 line数に応じて色変化 */
/* debug */void catchData();
int main(void){
/* setting */{
initscr();
cbreak();
noecho();
}
/* color setting */{
start_color();
colorSetting();
attrset(COLOR_PAIR(1));
}
timeout(-1);
/* check error */
int e = checkError();
if(e){
endwin();
return e;
}
drawBox(3);
timeout(0);
rotate(SETTING);
/* end */{
timeout(-1);
getch();
endwin();
}
return 0;
}
void flashBox(){
int num = SPEED>60000 ? 1 : 2;
int i;
drawBox(0);usleep(SPEED * 3);clear();
for(i=0; i<num; i++){
drawBox(1);usleep(SPEED * 3);clear();
drawBox(2);usleep(SPEED * 3);clear();
drawBox(3);usleep(SPEED * 3);clear();
drawBox(2);usleep(SPEED * 3);clear();
drawBox(1);usleep(SPEED * 3);clear();
drawBox(0);usleep(SPEED * 3);clear();
}
}
int checkError(){
int winx, winy;
getmaxyx(stdscr, winy, winx);
/* 高さ不足 */
if(winy < NEED_HEIGHT){
printw("SHORT HEIGHT ERROT!! NEED %d, now:%d", NEED_HEIGHT, winy);
getch();
return -1;
}
/* 幅不足 */
if(winx < NEED_WIDTH){
printw("SHORT WIDTH ERROR! NEED %d, now:%d", NEED_WIDTH, winx);
getch();
return -1;
}
/* WINDOW SIZE CLEAR */
/* GAME START表示 */
char *str1 = "GAME START!!";
char *str2 = "PUSH ANY KEY!!";
mvaddstr(winy/2, (winx-strlen(str1))/2, str1);
mvaddstr(winy/2+1, (winx-strlen(str2))/2, str2);
getch();
clear();
/* 演出 */
flashBox();
/* チェッカー初期化 */
resetChecker();
/* スロット表示 *//*
int i;
for(i=-1-REEL_WIDTH*1.5; i<=1+REEL_WIDTH/2; i+=REEL_WIDTH+1){
putReel(winy/2+2, winx/2+i, 3, 0);
}*/
return 0;
}
void resetChecker(){
checker[0] = checker[1] = checker[2] = SPIN;
}
void colorSetting(){
init_pair(1, colorYELLOW, colorBLACK);
init_pair(2, colorRED, colorBLACK);
init_pair(3, colorBLUE, colorBLACK);
init_pair(4, colorGREEN, colorBLACK);
init_pair(5, colorWHITE, colorBLACK);
}
void putReel(int winy, int winx, int rn, int num){
int i;
for(i=0; i<5; i++){
mvaddstr(winy+i, winx, illust[reel[rn][num]*5+i]);
}
}
void putReel_halfAfter(int winy, int winx, int rn, int num){
int i;
/* num+1番のリールを下2列表示 */
int numPlusOne = num==REEL_LONG?0:num+1;
for(i=0; i<2; i++){
mvaddstr(winy+i, winx, illust[reel[rn][numPlusOne]*5+i+3]);
}
/* 1行空行入れて 今の番号のリールを上2列表示 */
mvaddstr(winy+2, winx, " ");
for(i=0; i<2; i++){
mvaddstr(winy+i+3, winx, illust[reel[rn][num]*5+i]);
}
}
void putReel_threeLine(int winy, int winx, int rn, int num){
putReel(winy+6, winx, rn, num==0?REEL_LONG:num-1);
/* num-1番のイラストを表示 */
/* 第4変数 num-1を入れたいがnumが0ならはREEL_LONGにする */
putReel(winy, winx, rn, num);
putReel(winy-6, winx, rn, num==REEL_LONG?0:num+1);
move(0, 0);
refresh();
}
void putReel_threeAfter(int winy, int winx, int rn, int num){
putReel_halfAfter(winy+6, winx, rn, num==0?REEL_LONG:num-1);
putReel_halfAfter(winy, winx, rn, num);
putReel_halfAfter(winy-6, winx, rn, num==REEL_LONG?0:num+1);
move(0, 0);
refresh();
}
void rotate(int difficulty){
srand((unsigned int)time(NULL));
if(difficulty > ILLUSTs)difficulty = ILLUSTs;
int j = rand()%difficulty;
int k = rand()%difficulty;
int l = rand()%difficulty;
//mvprintw(0, 0, "Start position\t%d\t%d\t%d", j, k, l);
while(1){
/* スピン中 現在のイラストと, 1/2段進んだイラストをそれぞれ表示 */
if(checker[0]==SPIN) putReel_threeLine(1+5+1, 8, 0, j);
if(checker[1]==SPIN) putReel_threeLine(1+5+1, 8+REEL_WIDTH+3, 1, k);
if(checker[2]==SPIN) putReel_threeLine(1+5+1, 8+REEL_WIDTH+3+REEL_WIDTH+3, 2, l);
usleep(SPEED);
if(checker[0]==SPIN) putReel_threeAfter(1+5+1, 8, 0, j);
if(checker[1]==SPIN) putReel_threeAfter(1+5+1, 8+REEL_WIDTH+3, 1, k);
if(checker[2]==SPIN) putReel_threeAfter(1+5+1, 8+REEL_WIDTH+3+REEL_WIDTH+3, 2, l);
switch(getch()){
case 'j':
checker[0] = difficultyGate(difficulty, 0, j);
putReel_threeLine(1+5+1, 8, 0, checker[0]);
catchData();
break;
case 'k':
checker[1] = difficultyGate(difficulty, 1, k);
putReel_threeLine(1+5+1, 8+REEL_WIDTH+3, 1, checker[1]);
catchData();
break;
case 'l':
checker[2] = difficultyGate(difficulty, 2, l);
putReel_threeLine(1+5+1, 8+REEL_WIDTH+3+REEL_WIDTH+3, 2, checker[2]);
catchData();
break;
case 'q':goto END;
}
if(checker[0]!=SPIN&&checker[1]!=SPIN&&checker[2]!=SPIN){
resetChecker();
while(1){
char next = getch();
switch(next){
case 'q':goto END;break;
case 'i':goto NEXT;break;
}
}
NEXT:;
}
if(checker[0]==SPIN)j++;
if(checker[1]==SPIN)k++;
if(checker[2]==SPIN)l++;
usleep(SPEED);
if(j==REEL_LONG)j=0; if(k==REEL_LONG)k=0; if(l==REEL_LONG)l=0;
}
END:;
}
int difficultyGate(int difficulty, int rn, int num){
while(reel[rn][num]>=difficulty/*1~7*/){
num++;
if(num==REEL_LONG)num=0;
switch(rn){
case 0:
putReel_threeLine(1+5+1, 8, 0, num); usleep(1000*difficulty);
putReel_threeAfter(1+5+1, 8, 0, num); usleep(1000*difficulty); break;
case 1:
putReel_threeLine(1+5+1, 8+REEL_WIDTH+3, 1, num); usleep(1000*difficulty);
putReel_threeAfter(1+5+1, 8+REEL_WIDTH+3, 1, num); usleep(1000*difficulty); break;
case 2:
putReel_threeLine(1+5+1, 8+REEL_WIDTH+3+REEL_WIDTH+3, 2, num); usleep(1000*difficulty);
putReel_threeAfter(1+5+1, 8+REEL_WIDTH+3+REEL_WIDTH+3, 2, num); usleep(1000*difficulty); break;
}
}
return num;
}
int decisionGate(int dicision, int rn, int num){
while(reel[rn][num]!=dicision/*1~7*/-1){
num++;
if(num==REEL_LONG)num=0;
}
return num;
}
void drawBox(int line){
int i;
/* line 1 */
attrset(COLOR_PAIR( line>0 ? 2 : 5 ));
for(i=0; i<7; i++){
mvaddstr(6+i, 0, Box_1[i]);
}
/* line 2 */
attrset(COLOR_PAIR( line>1 ? 3 : 5 )); /* lineが2か3なら青で,lineが1なら白で */
for(i=0; i<6; i++){
mvaddstr(0+i, 0, Box_2up[i]);
}
for(i=0; i<6; i++){
mvaddstr(13+i, 0, Box_2dn[i]);
}
/* line 3*/
attrset(COLOR_PAIR( line>2 ? 4 : 5 ));
for(i=0; i<3; i++){
mvaddstr(0+i, 0, Box_3LU[i]);
mvaddstr(0+i, 34, Box_3RU[i]);
}
for(i=0; i<3; i++){
mvaddstr(16+i, 0, Box_3LD[i]);
mvaddstr(16+i, 34, Box_3RD[i]);
}
/* line bottun */
attrset(COLOR_PAIR(5));
for(i=0; i<3; i++){
mvaddstr(19+i, 0, Box_Button[i]);
}
attrset(COLOR_PAIR(1));/* 戻さないとスロットの色が変になる */
refresh();
}
void catchData(){
/* debug */
int j = checker[0]==SPIN ? 99 : checker[0];
int k = checker[1]==SPIN ? 99 : checker[1];
int l = checker[2]==SPIN ? 99 : checker[2];
mvprintw(20, 0, " [%2d] [%2d] [%2d]", j, k, l);
}
@oha-yashi
Copy link
Author

oha-yashi commented Aug 27, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment