Created
May 19, 2022 06:55
-
-
Save seya128/ed314e415e5670d15248ab26b481ac0e to your computer and use it in GitHub Desktop.
C言語サンプル スロットゲーム(数字を大きくした)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
チーム名: | |
メンバー: 瀬谷 | |
タイトル: スロットゲーム(BIG数字) | |
内容: | |
画面上に3つの大きな数字を表示。 | |
それぞれが1~9まで変化しながら表示されている。 | |
[1][2][3]キーでストップ。 | |
[s]キーでスタート。 | |
*/ | |
#include <stdio.h> | |
#include <conio.h> | |
#include <Windows.h> | |
#include <stdlib.h> | |
void cursor(int x, int y); | |
int keyin(void); | |
int random(int max); | |
void print_BigNumber(int x, int y, int num); | |
int main(void) | |
{ | |
int ch; | |
int numbers[3] = { 1, 2, 3 }; | |
int stop_flag[3] = { 0, 0, 0 }; /* 0:回転中 1:停止中 */ | |
int i; | |
/* | |
* Visual Studio 2019の「F5」で動かすと動くけど、 | |
* 作成されたexeを起動するとエスケープシーケンスが動かない対策 | |
*/ | |
HANDLE stdOut = GetStdHandle(STD_OUTPUT_HANDLE); | |
DWORD consoleMode = 0; | |
GetConsoleMode(stdOut, &consoleMode); | |
consoleMode = consoleMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING; | |
SetConsoleMode(stdOut, consoleMode); | |
/* 説明 */ | |
printf("-- スロットゲーム --\n"); | |
printf("\n"); | |
printf("[1][2][3]キー:停止\n"); | |
printf("[s]:スタート\n"); | |
printf("同じ数字をそろえよう!\n"); | |
while (1) { | |
/* キー入力処理 */ | |
ch = keyin(); | |
if (ch == '1') { | |
stop_flag[0] = 1; | |
} | |
else if (ch == '2') { | |
stop_flag[1] = 1; | |
} | |
else if (ch == '3') { | |
stop_flag[2] = 1; | |
} | |
else if (ch == 's') { | |
stop_flag[0] = 0; | |
stop_flag[1] = 0; | |
stop_flag[2] = 0; | |
} | |
/* 数字処理 */ | |
for (i = 0; i < 3; i++) { | |
if (stop_flag[i] == 0) { | |
/* 数字更新 */ | |
numbers[i] ++; | |
if (numbers[i] > 9) { | |
numbers[i] = 1; | |
} | |
/* 数字表示 */ | |
print_BigNumber(4 + i*12, 10, numbers[i]); | |
} | |
} | |
/* 揃ったかどうか判定 */ | |
if (stop_flag[0] == 1 && stop_flag[1] == 1 && stop_flag[2] == 1) { /* 全部停止 */ | |
if (numbers[0] == numbers[1] && numbers[1] == numbers[2]) { /* 全部の数字が同じ */ | |
cursor(16, 20); | |
printf("当たり!"); | |
} | |
else { | |
cursor(16, 20); | |
printf("はずれ"); | |
} | |
} | |
else { | |
cursor(16, 20); | |
printf(" "); | |
} | |
Sleep(100); | |
} | |
return 0; | |
} | |
void print_BigNumber(int x, int y, int num) | |
{ | |
switch (num) | |
{ | |
case 1: | |
cursor(x, y + 0); printf(" ■ "); | |
cursor(x, y + 1); printf(" ■■ "); | |
cursor(x, y + 2); printf(" ■ "); | |
cursor(x, y + 3); printf(" ■ "); | |
cursor(x, y + 4); printf(" ■ "); | |
cursor(x, y + 5); printf(" ■ "); | |
cursor(x, y + 6); printf(" ■■■"); | |
break; | |
case 2: | |
cursor(x, y + 0); printf("■■■■"); | |
cursor(x, y + 1); printf(" ■"); | |
cursor(x, y + 2); printf(" ■"); | |
cursor(x, y + 3); printf("■■■■"); | |
cursor(x, y + 4); printf("■ "); | |
cursor(x, y + 5); printf("■ "); | |
cursor(x, y + 6); printf("■■■■"); | |
break; | |
case 3: | |
cursor(x, y + 0); printf("■■■■"); | |
cursor(x, y + 1); printf(" ■"); | |
cursor(x, y + 2); printf(" ■"); | |
cursor(x, y + 3); printf("■■■■"); | |
cursor(x, y + 4); printf(" ■"); | |
cursor(x, y + 5); printf(" ■"); | |
cursor(x, y + 6); printf("■■■■"); | |
break; | |
case 4: | |
cursor(x, y + 0); printf("■ ■"); | |
cursor(x, y + 1); printf("■ ■"); | |
cursor(x, y + 2); printf("■ ■"); | |
cursor(x, y + 3); printf("■■■■"); | |
cursor(x, y + 4); printf(" ■"); | |
cursor(x, y + 5); printf(" ■"); | |
cursor(x, y + 6); printf(" ■"); | |
break; | |
case 5: | |
cursor(x, y + 0); printf("■■■■"); | |
cursor(x, y + 1); printf("■ "); | |
cursor(x, y + 2); printf("■ "); | |
cursor(x, y + 3); printf("■■■■"); | |
cursor(x, y + 4); printf(" ■"); | |
cursor(x, y + 5); printf(" ■"); | |
cursor(x, y + 6); printf("■■■■"); | |
break; | |
case 6: | |
cursor(x, y + 0); printf("■■■■"); | |
cursor(x, y + 1); printf("■ "); | |
cursor(x, y + 2); printf("■ "); | |
cursor(x, y + 3); printf("■■■■"); | |
cursor(x, y + 4); printf("■ ■"); | |
cursor(x, y + 5); printf("■ ■"); | |
cursor(x, y + 6); printf("■■■■"); | |
break; | |
case 7: | |
cursor(x, y + 0); printf("■■■■"); | |
cursor(x, y + 1); printf(" ■"); | |
cursor(x, y + 2); printf(" ■"); | |
cursor(x, y + 3); printf(" ■"); | |
cursor(x, y + 4); printf(" ■"); | |
cursor(x, y + 5); printf(" ■"); | |
cursor(x, y + 6); printf(" ■"); | |
break; | |
case 8: | |
cursor(x, y + 0); printf("■■■■"); | |
cursor(x, y + 1); printf("■ ■"); | |
cursor(x, y + 2); printf("■ ■"); | |
cursor(x, y + 3); printf("■■■■"); | |
cursor(x, y + 4); printf("■ ■"); | |
cursor(x, y + 5); printf("■ ■"); | |
cursor(x, y + 6); printf("■■■■"); | |
break; | |
case 9: | |
cursor(x, y + 0); printf("■■■■"); | |
cursor(x, y + 1); printf("■ ■"); | |
cursor(x, y + 2); printf("■ ■"); | |
cursor(x, y + 3); printf("■■■■"); | |
cursor(x, y + 4); printf(" ■"); | |
cursor(x, y + 5); printf(" ■"); | |
cursor(x, y + 6); printf("■■■■"); | |
break; | |
default: | |
break; | |
} | |
} | |
void cursor(int x, int y) | |
{ | |
printf("\033[%d;%dH", y, x); | |
} | |
int keyin(void) | |
{ | |
int ch; | |
if (_kbhit() != 0) { | |
ch = _getch(); | |
} | |
else { | |
ch = 0; | |
} | |
return ch; | |
} | |
int random(int max) | |
{ | |
int r; | |
r = rand() % max; | |
return r; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment