Skip to content

Instantly share code, notes, and snippets.

@seya128
Created May 19, 2022 06:40
Show Gist options
  • Save seya128/cfacce633fbf33f637a52f2b775a76fe to your computer and use it in GitHub Desktop.
Save seya128/cfacce633fbf33f637a52f2b775a76fe to your computer and use it in GitHub Desktop.
C言語サンプル スロットゲーム
/*
チーム名:
メンバー: 瀬谷
タイトル: スロットゲーム
内容:
 画面上に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);
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;
}
/* 数字表示 */
cursor(10 + i*4, 10);
printf("%d", 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(12, 12);
printf("当たり!");
}
else {
cursor(12, 12);
printf("はずれ");
}
}
else {
cursor(12, 12);
printf(" ");
}
Sleep(100);
}
return 0;
}
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