타자게임 : 1. 단어등록하기 2. 게임하기

  1. 단어 도감 등록하기 → 게임에 필요한 단어를 입력하면 파일에 저장시키기
  2. 게임→파일에 저장되어 있는 단어 불러와서 랜덤으로 화면에 출력이 되고, 맞추면 포인트 획득
  3. 시간이 지날 수록 단어가 여러개씩 늘어나며 난이도 올리기
  4. 특정 난이도 되면 클리어

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
#include <string.h>

#define WORDLEN 64
#define MAX_WORD 20
#define FILE_NAME "TypingGame.txt"
#define CLEAR_COUNT 5

using namespace std;

void Search(FILE* file, int& correctcount)
{
	char temp[WORDLEN];
	char input[WORDLEN];


	printf("지금 까지 맞춘 정답 수 : %d\n", correctcount);
	printf("단어를 입력해주세요 : ");
	scanf("%s", input);

	fgets(temp, WORDLEN, file);

	while (fgets(temp, WORDLEN, file))
	{

		if (strcmp(temp, input))
		{
			printf("%s 찾았습니다!\n", input);
			correctcount++;
			Sleep(1000);
			system("cls");
			break;
		}
	}
}

void Save(FILE* file)
{
	char temp[WORDLEN];

	printf("단어 입력 : ");
	scanf_s("%s", temp, WORDLEN);

	fprintf(file, "%s ", temp);
}

void Load(FILE* file, char* find)
{

}

void SetWord(FILE* file)
{
	int wordcount = 0;
	printf("입력하고 싶은 단어의 갯수 : ");
	scanf("%d", &wordcount);
	
	CLEAR_COUNT == wordcount;

	for (int i = 0; i < wordcount; i++)
	{
		Save(file);
	}

	printf("등록 완료!");
	Sleep(1000);
	system("cls");
}

void TypingGame(FILE* file)
{
	int correctcount = 0;

	while (1)
	{
		Search(file, correctcount);

		if (correctcount == CLEAR_COUNT)
		{
			printf("목표 정답수에 도달하였습니다!\n");
			Sleep(1000);
			printf("Game Clear!\n");
			Sleep(1000);
			break;
		}
	}
}

void Menu(FILE* file)
{
	while (1)
	{
		int input = 0;
		printf("1. 단어 등록 2. 게임 시작\n");
		scanf("%d", &input);

		if (input == 1)
		{
			SetWord(file);
		}
		else if (input == 2)
		{
			TypingGame(file);
			break;
		}
	}

}

int main()
{
	FILE* file;
	fopen_s(&file, "TypingGame.txt", "r+"); // 읽기 전용
	printf("타자 게임을 시작하겠습니다!\n");
	Menu(file);



	fclose(file);
}

'C ++ > C++ 게임' 카테고리의 다른 글

221213 리그 게임 만들기  (0) 2022.12.13
221213 포켓몬스터 간단하게 만들기  (0) 2022.12.13
221208 슬라이드 게임 만들기  (0) 2022.12.08
221208 빙고 게임  (0) 2022.12.08
221207 숫자야구 게임 만들기  (0) 2022.12.07
  1. n * n 2차원 배열만들기
  2. 0부터 n * n - 1 까지 배열 채우기
  3. w a s d입력해서 0번을 상하 좌우로 이동하기
  4. 랜덤으로 0번을 많이 이동시켜서 셔플하기
  5. 게임 시작하면 0번을 움직여서 처음 모양으로 맞추면 게임 클리어
  6. 고급 : 시간제한이나 추가해보기

구현 : 5x5로 슬라이드 게임 구현

0자리는 공백으로 두어 가시성 좋게 구현함

0이 가지 못하는 벽에 부딪히면 부딪혔다고 알림 뜨게 구현

 

#include <iostream>
#include <Windows.h>
#include <conio.h>
#include <vector>
#include <algorithm>
using namespace std;

#define GAMESIZE 5
#define MAX_NUM 25

void SetRandomSlideGame(vector<int>& arr) // 숫자 랜덤 셔플
{
	for (int i = 0; i < GAMESIZE; i++)
	{
		for (int j = 0; j < GAMESIZE; j++)
		{
			arr.push_back(i * GAMESIZE + j);
		}
	}

	random_shuffle(arr.begin(), arr.end());

}

void PrintSlideGame(vector<int> arr) // 게임판 출력
{
	int count = 0;
	for (int i = 0; i < MAX_NUM; i++)
	{
		count++;

		if (arr[i] == 0)
		{
			cout << "   ";
		}
		else
		{
			printf("%.2d ", arr[i]);
		}

		if (count == GAMESIZE)
		{
			count = 0;
			cout << endl;
		}

	}

}

int MoveZero(vector<int>& slidegame)
{
	int zeroindex = find(slidegame.begin(), slidegame.end(), 0) - slidegame.begin();
	int temp = 0;
	string inputkey;

	while (1)
	{
		if (_kbhit())
		{
			inputkey = _getch();

			if (inputkey == "w" || inputkey == "W")
			{
				if (zeroindex == 0 || zeroindex == 1 || zeroindex == 2 ||
					zeroindex == 3 || zeroindex == 4)
				{
					cout << "0이 벽에 막혔습니다." << endl;
					Sleep(1000);
					break;
				}

				temp = slidegame[zeroindex];
				slidegame[zeroindex] = slidegame[zeroindex - 5];
				slidegame[zeroindex - 5] = temp;
				break;

			}
			else if (inputkey == "a" || inputkey == "A")
			{
				if (zeroindex == 0 || zeroindex == 5 || zeroindex == 10 ||
					zeroindex == 15 || zeroindex == 20)
				{
					cout << "0이 벽에 막혔습니다." << endl;
					Sleep(1000);
					break;
				}

				temp = slidegame[zeroindex];
				slidegame[zeroindex] = slidegame[zeroindex - 1];
				slidegame[zeroindex - 1] = temp;
				break;
			}
			else if (inputkey == "s" || inputkey == "S")
			{
				if (zeroindex == 20 || zeroindex == 21 || zeroindex == 22 ||
					zeroindex == 23 || zeroindex == 24)
				{
					cout << "0이 벽에 막혔습니다." << endl;
					Sleep(1000);
					break;
				}

				temp = slidegame[zeroindex];
				slidegame[zeroindex] = slidegame[zeroindex + 5];
				slidegame[zeroindex + 5] = temp;
				break;
			}
			else if (inputkey == "d" || inputkey == "D")
			{
				if (zeroindex == 4 || zeroindex == 9 || zeroindex == 14 ||
					zeroindex == 19 || zeroindex == 24)
				{
					cout << "0이 벽에 막혔습니다." << endl;
					Sleep(1000);
					break;
				}

				temp = slidegame[zeroindex];
				slidegame[zeroindex] = slidegame[zeroindex + 1];
				slidegame[zeroindex + 1] = temp;
				break;
			}
		}
	}
	return 0;
}

bool CheckSlideGame(vector<int> slidegame)
{
	vector<int> compare;
	for (int i = 0; i < MAX_NUM; i++)
	{
		compare.push_back(i);
	}

	for (int i = 0; i < MAX_NUM; i++)
	{
		if (compare[i] != slidegame[i])
		{
			return false;
		}
	}

	return true;
}

int main()
{
	srand(time(NULL));
	vector<int> slidegame;
	SetRandomSlideGame(slidegame);
	string inputkey;

	cout << "슬라이드 게임을 시작하겠습니다!" << endl;
	Sleep(1000);
	cout << "무작위로 셔플된 정수 속에서 0을 움직여서 수를 순서대로 배치하면 게임이 클리어됩니다!" << endl;
	Sleep(1000);

	while (true)
	{
		PrintSlideGame(slidegame);
		MoveZero(slidegame);
		CheckSlideGame(slidegame);

		if (CheckSlideGame(slidegame) == true)
		{
			system("cls");
			PrintSlideGame(slidegame);
			cout << endl;
			cout << "모든 정수를 순서대로 배치했습니다!" << endl;
			Sleep(1000);
			cout << "Game Clear!" << endl;
			Sleep(1000);
			break;
		}
		system("cls");
	}
}

'C ++ > C++ 게임' 카테고리의 다른 글

221213 포켓몬스터 간단하게 만들기  (0) 2022.12.13
221209 타자 치기 게임  (0) 2022.12.09
221208 빙고 게임  (0) 2022.12.08
221207 숫자야구 게임 만들기  (0) 2022.12.07
221206 다이아몬드 별찍기  (0) 2022.12.06
  1. 1~100사이의 랜덤숫자를 중복 없이 채워넣기
  2. 2차원 배열을 화면에 출력

      01 02 58 82 92

       ~

       ~

       ~

   3. 숫자를 입력해서 일치하는게있으면 0이나 특수문자로 바꾸기

   4.빙고 갯수 체크해서 5개 이상 빙고가 완성되면 게임 클리어

고급 : 배열 출력을 다 가리고 시도 횟수 추가해서, 횟수 안에 빙고 맞추면 게임 클리어

 

구현 : 

1 ~ 25까지의 숫자가 중복없이 생성 후 랜덤으로 셔플되어 출력됨 ( 보이는건 X로 보임 )

20회의 찬스안에 3빙고를 달성해야 게임 클리어

 

#include <iostream>
#include <Windows.h>
#include <vector>
#include <algorithm>
using namespace std;

#define BINGOSIZE 5
#define MAX_NUM 25
#define MIN_NUM 1

struct BingoCell
{
	int cellnum;
	string cellstat = "X";
};

void SetRandomBingo(vector<BingoCell>& bingo) // 빙고 숫자 랜덤 셔플
{
	for (int i = 0; i < BINGOSIZE; i++)
	{
		for (int j = 0; j < BINGOSIZE; j++)
		{
			bingo.push_back({ i * BINGOSIZE + j + 1,"X" });
		}
	}

	random_shuffle(bingo.begin(), bingo.end());

}

void PrintBingo(vector<BingoCell> bingo) // 빙고판 출력
{
	int count = 0;
	for (int i = 0; i < MAX_NUM; i++)
	{
		count++;
		cout << bingo[i].cellstat << " ";

		if (count == BINGOSIZE)
		{
			count = 0;
			cout << endl;
		}

	}
	cout << endl;
}

void CheckInput(int input, vector<BingoCell>& bingo)
{
	for (int i = 0; i < MAX_NUM; i++)
	{
		if (input == bingo[i].cellnum)
		{
			bingo[i].cellstat = "O";
		}
	}

}

int CheckBingo(vector<BingoCell> bingo)
{
	int checkbingocount = 0;
	for (int i = 0; i < BINGOSIZE; i++) // 가로 빙고
	{
		if (bingo[i * BINGOSIZE].cellstat == "O" &&
			bingo[i * BINGOSIZE + 1].cellstat == "O" &&
			bingo[i * BINGOSIZE + 2].cellstat == "O" &&
			bingo[i * BINGOSIZE + 3].cellstat == "O" &&
			bingo[i * BINGOSIZE + 4].cellstat == "O")
		{
			checkbingocount++;
		}

		if (bingo[i].cellstat == "O" && // 세로 빙고
			bingo[i + BINGOSIZE].cellstat == "O" &&
			bingo[i + BINGOSIZE * 2].cellstat == "O" &&
			bingo[i + BINGOSIZE * 3].cellstat == "O" &&
			bingo[i + BINGOSIZE * 4].cellstat == "O")
		{
			checkbingocount++;
		}
	}

	if (bingo[0].cellstat == "O" && // 대각선 빙고
		bingo[6].cellstat == "O" &&
		bingo[12].cellstat == "O" &&
		bingo[18].cellstat == "O" &&
		bingo[24].cellstat == "O")
	{
		checkbingocount++;
	}

	if (bingo[4].cellstat == "O" && // 대각선 빙고
		bingo[8].cellstat == "O" &&
		bingo[12].cellstat == "O" &&
		bingo[16].cellstat == "O" &&
		bingo[20].cellstat == "O")
	{
		checkbingocount++;
	}
	return checkbingocount;
}

int main()
{
	srand(time(NULL));
	vector<BingoCell> bingo;
	SetRandomBingo(bingo);
	int input;
	int bingocount = 0;
	int chance = BINGOSIZE * 4;

	cout << "빙고 게임을 시작하겠습니다!" << endl;
	Sleep(1000);

	while (true)
	{
		PrintBingo(bingo);
		cout << "1부터 25까지의 숫자를 입력해주세요" << endl;
		cout << "남은 찬스 : " << chance << endl;
		cout << "현재까지 빙고 갯수 : " << bingocount << endl;
		cin >> input;
		CheckInput(input, bingo);
		Sleep(1000);
		system("cls");
		bingocount = CheckBingo(bingo);

		if (chance == 0)
		{
			cout << "찬스를 전부 사용했습니다." << endl;
			Sleep(1000);
			cout << "Game Over..." << endl;
			Sleep(1000);
			break;
		}

		if (bingocount >= 3)
		{
			PrintBingo(bingo);
			cout << endl;
			cout << "빙고 3개를 완성했습니다." << endl;
			Sleep(1000);
			cout << "Game Clear!" << endl;
			Sleep(1000);
			break;
		}
		chance--;
	}

}

'C ++ > C++ 게임' 카테고리의 다른 글

221209 타자 치기 게임  (0) 2022.12.09
221208 슬라이드 게임 만들기  (0) 2022.12.08
221207 숫자야구 게임 만들기  (0) 2022.12.07
221206 다이아몬드 별찍기  (0) 2022.12.06
221206 Up&Down 게임 만들기  (0) 2022.12.06
  1. 1 ~ 9까지의 랜덤 숫자 3개 생성 ( 중복 가능, 중복 없이 ), 141 OR 194
  2. 1~9 사이의 랜덤 숫자 3개 입력받기
  3. 결과 출력 : 자릿수랑 숫자가 같으면 ST, 자릿수는다르고 숫자만 같으면 볼

        EX) Random : 1 9 4

        111 → 1s 2b

        248 → 1b

        333 → 1아웃

  1. 3스트라이크 되면 게임 클리어, 3아웃 또는 시도횟수 초과하면 게임 오버

구현 : 

플레이어가 중복된 정수를 입력하면 예외처리하여 다시 입력하게 구현

컴퓨터는 중복되지 않는 세 정수를 선택

주어진 토큰 5개동안 3스트라이크를 만들지 못하면 게임오버

 

#include <iostream>
#include <Windows.h>
#include <vector>
#include <algorithm>
using namespace std;

class BaseballGame
{
public:
	int PlayBall(vector<int> playerpick, vector<int> computerpick);

	int GetStrike() { return strike; }
	int GetOut() { return out; }
	int GetToken() { return token; }


private:
	int strike = 0;
	int ball = 0;
	int out = 0;
	int token = 5;
};

int BaseballGame::PlayBall(vector<int> playerpick, vector<int> computerpick)
{
	for (int i = 0; i < 3; i++) // 스트라이크,볼 계산
	{
		for (int j = 0; j < 3; j++)
		{
			if (i == j)
			{
				if (playerpick[i] == computerpick[j])
				{
					strike++;
				}
			}

			if (i != j)
			{
				if (playerpick[i] == computerpick[j])
				{
					ball++;

				}
			}
		}
	}

	if (strike == 0 && ball == 0)
	{
		out++;
	}

	cout << ball << "B " << strike << "S!!" << endl;
	Sleep(1000);
	cout << "아웃 카운트는 " << out << "개입니다." << endl;
	Sleep(1000);

	if (strike == 3) // 3S 승리
	{
		cout << endl;
		cout << "STRIKE 3!! 게임에서 승리하였습니다!" << endl;
		Sleep(1000);
		cout << "컴퓨터의 숫자는 " << computerpick[0] << " " << computerpick[1] << " " << computerpick[2] << " 이였습니다." << endl;
		Sleep(1000);
		cout << "GAME CLEAR!" << endl;
		Sleep(1000);
		return 0;
	}

	strike = 0;
	ball = 0;

	if (out == 3) // 3아웃 게임오버
	{
		cout << endl;
		cout << "3OUT! 게임이 종료되었습니다." << endl;
		Sleep(1000);
		cout << "컴퓨터의 숫자는 " << computerpick[0] << " " << computerpick[1] << " " << computerpick[2] << " 이였습니다." << endl;
		Sleep(1000);
		cout << "GAME OVER..." << endl;
		Sleep(1000);
		return 0;
	}

	token--;
	cout << "토큰이 1개 소모되었습니다. ( 남은 토큰 : " << token << " )" << endl;
	Sleep(1000);

	if (token == 0) // 토큰 모두 소모
	{
		cout << endl;
		cout << "토큰이 전부 소모되었습니다." << endl;
		Sleep(1000);
		cout << "컴퓨터의 숫자는 " << computerpick[0] << " " << computerpick[1] << " " << computerpick[2] << " 이였습니다." << endl;
		Sleep(1000);
		cout << "GAME OVER..." << endl;
		Sleep(1000);
		return 0;
	}
}

vector<int> PlayerPick(vector<int> pick) // 플레이어 중복 정수 체크 함수
{
	while (true)
	{
		pick.erase(pick.begin(), pick.end());
		cout << endl;
		cout << "1부터 9까지 서로 다른 정수 3개를 입력해주세요." << endl;

		for (int i = 0; i < 3; i++)
		{
			int temp;
			cin >> temp;
			pick.push_back(temp);
		}
		vector<int> temp;
		temp = pick;
		sort(pick.begin(), pick.end());
		pick.erase(unique(pick.begin(), pick.end()), pick.end());

		if (pick.size() != 3)
		{
			cout << "같은 정수가 있습니다." << endl;
			Sleep(1000);
			cout << "다시 입력해주세요." << endl;
			Sleep(1000);
		}
		else if (pick.size() == 3)
		{
			pick = temp;
			break;
		}

	}

	return pick;
}

vector<int> ComputerPick(vector<int> pick) // 컴퓨터 중복 정수 체크 및 랜덤 정수 생성
{
	srand(time(NULL));
	pick.erase(pick.begin(), pick.end());
	for (int i = 0; i < 3; i++)
	{
		pick.push_back(rand() % 9 + 1);
	}

	while (1)
	{
		sort(pick.begin(), pick.end());
		pick.erase(unique(pick.begin(), pick.end()), pick.end());
		random_shuffle(pick.begin(), pick.end());

		if (pick.size() == 3)
		{
			break;
		}
		else if (pick.size() != 3)
		{
			pick.push_back(rand() % 9 + 1);
		}
	}
	return pick;
}

int main()
{
	srand(time(NULL));
	BaseballGame bb;
	vector<int> playerpick;
	vector<int> computerpick = ComputerPick(computerpick);

	cout << "숫자 야구 게임을 시작하겠습니다." << endl;
	Sleep(1000);
	cout << "숫자를 선택할때마다 토큰이 1개씩 소모됩니다." << endl;
	Sleep(1000);
	cout << "토큰을 모두 소모하기 전에 숫자와 숫자의 위치를 맞추면 승리합니다." << endl;
	Sleep(1000);
	cout << "3아웃이 되면 게임오버가 되며, 토큰을 모두 소모해도 게임오버가 됩니다." << endl;
	Sleep(1000);

	while (true)
	{
		playerpick = PlayerPick(playerpick);
		cout << "플레이어의 선택 : " << playerpick[0] << " " << playerpick[1] << " " << playerpick[2] << endl;
		bb.PlayBall(playerpick, computerpick);

		if (bb.GetOut() == 3)
		{
			break;
		}
		else if (bb.GetToken() == 0)
		{
			break;
		}
		else if (bb.GetStrike() == 3)
		{
			break;
		}
	}
}

'C ++ > C++ 게임' 카테고리의 다른 글

221208 슬라이드 게임 만들기  (0) 2022.12.08
221208 빙고 게임  (0) 2022.12.08
221206 다이아몬드 별찍기  (0) 2022.12.06
221206 Up&Down 게임 만들기  (0) 2022.12.06
221205 가위바위보 게임 만들기  (0) 2022.12.06
  1. 다이아몬드 찍기
    1. 1이상의 홀수를 입력받기
    2. ex) 3→  ( 중간 가장 긴길이 3으로  )

                            ★

                        ★★★

                           

#include <iostream>
#include <Windows.h>
using namespace std;
void Diamond(int odd)
{
	for (int i = 0; i <= odd/2; i++)
	{
		for (int j = i; j < odd/2; j++)
		{
			cout << " ";
		}

		for (int j = odd - 1 - i * 2; j < odd; j++)
		{
			cout << "*";
		}
		cout << endl;
	}

	for (int i = odd/2 - 1; i >= 0; i--)
	{
		for (int j = i; j < odd/2; j++)
		{
			cout << " ";
		}

		for (int j = odd - 1 - i * 2; j < odd; j++)
		{
			cout << "*";
		}
		cout << endl;
	}
}
int main()
{
	int playerNum;
	cout << "홀수를 입력해주세요" << endl;
	cin >> playerNum;
	while (playerNum % 2 == 0)
	{
		cout << "짝수를 입력하셨습니다." << endl;
		Sleep(1000);
		cout << "다시 입력해주세요." << endl;
		Sleep(1000);
		system("cls");
		cout << "홀수를 입력해주세요" << endl;
		cin >> playerNum;
		if (playerNum % 2 == 1)
		{
			break;
		}
	}
	Diamond(playerNum);
}

+ Recent posts