diff --git a/README.md b/README.md new file mode 100644 index 0000000..9222850 --- /dev/null +++ b/README.md @@ -0,0 +1,7 @@ +ss1_TicTacToeEx +=============== +スキルゼミ第1回 拡張○×ゲーム + +更新履歴 +- 2016.10.28 ver 1.0 + - 基本プログラム公開 diff --git a/TicTacToeEx.sln b/TicTacToeEx.sln new file mode 100644 index 0000000..5934555 --- /dev/null +++ b/TicTacToeEx.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25123.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "TicTacToeEx", "TicTacToeEx\TicTacToeEx.vcxproj", "{4AF55B07-0A19-42C8-A704-814E682E44CC}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Debug|x64.ActiveCfg = Debug|x64 + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Debug|x64.Build.0 = Debug|x64 + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Debug|x86.ActiveCfg = Debug|Win32 + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Debug|x86.Build.0 = Debug|Win32 + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Release|x64.ActiveCfg = Release|x64 + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Release|x64.Build.0 = Release|x64 + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Release|x86.ActiveCfg = Release|Win32 + {4AF55B07-0A19-42C8-A704-814E682E44CC}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/TicTacToeEx/T3EBoard.cpp b/TicTacToeEx/T3EBoard.cpp new file mode 100644 index 0000000..fda9f09 --- /dev/null +++ b/TicTacToeEx/T3EBoard.cpp @@ -0,0 +1,277 @@ +#include "T3EBoard.h" +#include +#include +#include + +// �R���X�g���N�^ +T3EBoard::T3EBoard() +{ + srand((unsigned int)clock()); + this->Reset(); +} + +// �f�X�g���N�^ +T3EBoard::~T3EBoard() +{ +} + +// �Q�[�������� +void T3EBoard::Reset() +{ + m_Turn = 1; + m_NextPlayer = PLAYER1; + for (int i = 0; i < BOARD_SIZE; i++) { + m_Board[i] = NONE; + } +} + +// �Q�[���i�s +void T3EBoard::Play() +{ + // �Q�[�������� + std::cout << "�g���Ł��~ �Q�[��" << std::endl << std::endl; + std::cout << "���� 0:�l�� 1:CPU ?"; + std::cin >> m_Player[PLAYER1]; + std::cout << "���� 0:�l�� 1:CPU ?"; + std::cin >> m_Player[PLAYER2]; + std::cout << "�n�߁I" << std::endl; + + // �^�[���i�s + do { + // �^�[����� + std::cout << std::endl << "�^�[�� " << m_Turn << std::endl; + this->ShowBoard(); + std::cout << (m_NextPlayer == PLAYER1 ? "���" : "���") + << "�̔Ԃł�" << std::endl; + + int place = (m_Player[m_NextPlayer] == PLAYER_HUMAN ? Human() : CPU()); + this->SetStone(place); + int winner = this->CheckWin(); + if (winner >= 0) { + this->ShowBoard(); + std::cout << std::endl << (m_NextPlayer == PLAYER1 ? "���" : "���") + << " ����" << std::endl; + break; + } + + // ���̔Ԃ� + m_NextPlayer = 1 - m_NextPlayer; + if (m_NextPlayer == PLAYER1) m_Turn ++; + if (m_Turn >= MAX_TURNS) std::cout << std::endl << "���������I" << std::endl; + } while (m_Turn < MAX_TURNS); + + std::cin.ignore(std::cin.rdbuf()->in_avail()); // ���̓o�b�t�@�N���A + std::cin.get(); // �I���O�̃L�[���͑҂� +} + +// CPU�̎v�l�A���S���Y�� +// �߂�l�F�R�}��łꏊ +int T3EBoard::CPU() +{ + int place = 0; + int i[11],j; + int tempPlace[11]; + int ZeroCount=0,ZeroCount2=0; + int ImageBoard[BOARD_SIZE], ImageTurn,ImageNextPlayer,Me; + //���s�Ǘ��z�� + int row[11], col[11]; + int check[11]; + int sign[2] = { STONE1, STONE2 }; + + + //���� + ImageTurn = m_Turn; + ImageNextPlayer = m_NextPlayer; + printf("ImageNext = %d\n", ImageNextPlayer); + //�����͐��̐�,����͕��̐��ŕ��� + if (ImageNextPlayer == PLAYER1) { + printf("���͐��\n"); + for (i[0] = 0;i[0] < BOARD_SIZE;i[0]++) { + ImageBoard[i[0]] = m_Board[i[0]]; + } + } + else { + printf("���͌��\n"); + for (i[0] = 0;i[0] < BOARD_SIZE;i[0]++) { + ImageBoard[i[0]] = (-1)*m_Board[i[0]]; + } + } + + + //�Տ�̉����Ȃ��ꏊ�𐔂���B + for (i[0] = 0;i[0] < BOARD_SIZE;i[0]++) { + if (ImageBoard[i[0]] == 0)ZeroCount++; + } + + //printf("ZERO=%d\n", ZeroCount); + + //Minmax�@�̋L�q + //���݂̃^�[���S�ʂ莎��for depth=0 + for (i[0] = 0;i[0] < BOARD_SIZE;i[0]++) { + //printf("i = %d\n",i[0]); + //�����u���ĂȂ��ꏊ�����‚��� + if (ImageBoard[i[0]] == 0) { + //SetStone�̋L�q�P + ImageBoard[i[0]] = ImageTurn; + tempPlace[0] = i[0]; + + // �Â��R�}������ + if (ImageTurn > MAX_PIECE_ON_BOARD) { + int deleting = (ImageTurn - MAX_PIECE_ON_BOARD); + for (int i = 0; i < BOARD_SIZE; i++) { + if (ImageBoard[i] == deleting) ImageBoard[i] = 0; + } + } + + //���Ă�`�����X�������珟�� + // �΂ߕ���1 + for (row[0] = 0, check[0] = 0; row[0] < BOARD_HEIGHT; row[0]++) { + if (ImageBoard[row[0] * BOARD_WIDTH + row[0]] > 0) check[0]++; + } + if (check[0] == 3) { + printf("naname1\n");return i[0]; + } + + // �΂ߕ���2 + for (row[0] = 0, check[0] = 0; row[0] < BOARD_HEIGHT; row[0]++) { + if (ImageBoard[row[0] * BOARD_WIDTH + (BOARD_HEIGHT - 1 - row[0])] > 0) check[0]++; + } + if (check[0] == 3) { + printf("naname2\n");return i[0]; + } + + // ������ + for (row[0] = 0; row[0] < BOARD_HEIGHT; row[0]++) { + for (col[0] = 0, check[0] = 0; col[0] < BOARD_WIDTH; col[0]++) { + if (ImageBoard[row[0] * BOARD_WIDTH + col[0]] > 0) check[0]++; + } + if (check[0] == 3) { + printf("yoko\n");return i[0]; + } + + } + + + // �c���� + for (col[0] = 0; col[0] < BOARD_WIDTH; col[0]++) { + for (row[0] = 0, check[0] = 0; row[0] < BOARD_HEIGHT; row[0]++) { + if (ImageBoard[row[0] * BOARD_WIDTH + col[0]] > 0) check[0]++; + } + if (check[0] == 3) { + printf("tate\n");return i[0]; + } + } + + + //����̎�� depth=1 + ImageTurn++; + for (i[1] = 0;i[1] < BOARD_SIZE;i[1]++) { + if (ImageBoard[i[1]] == 0) { + + } + } + //������Ԃɖ߂� + ImageBoard[tempPlace[0]] = 0; + ImageTurn = m_Turn; + ImageNextPlayer = m_Turn; + } + } + + do { + place = rand() % BOARD_SIZE; + } while (m_Board[place] != NONE); + + std::cout << "CPU => " << place << std::endl; + return place; +} + +// �l�Ԃ̎�� +// �߂�l�F�R�}��łꏊ +int T3EBoard::Human() +{ + int place = 0; + while(1) { + std::cout << "0:���� 1:�� 2:�E�� 3:�� 4:���� 5:�E 6:���� 7:�� 8:�E��" << std::endl + << "�ǂ��ɑł��܂����H "; + std::cin >> place; + if (place >= 0 && place < BOARD_SIZE) { + if (m_Board[place] == 0) break; + } + } + + return place; +} + +// �ՂɃR�}��u�� +// ���� int place : �u���ʒu +void T3EBoard::SetStone(int place) +{ + int sign = m_NextPlayer == PLAYER1 ? STONE1 : STONE2; + m_Board[place] = m_Turn * sign; + + // �Â��R�}������ + if (m_Turn > MAX_PIECE_ON_BOARD) { + int deleting = (m_Turn - MAX_PIECE_ON_BOARD) * sign; + for (int i = 0; i < BOARD_SIZE; i++) { + if (m_Board[i] == deleting) m_Board[i] = 0; + } + } +} + +// �������� +// �߂�l�F-1 ������ 0(PLAYER1) ��菟�� 1(PLAYER2) ��菟�� +int T3EBoard::CheckWin() +{ + int row, col; + bool check; + int sign[2] = {STONE1, STONE2}; + + // �v���C���[���ɔ��� + for (int player = PLAYER1; player <= PLAYER2; player++) { + // �΂ߕ���1 + for (row = 0, check = true; row < BOARD_HEIGHT; row++) { + if (m_Board[row * BOARD_WIDTH + row] * sign[player] <= 0) check = false; + } + if (check) return player; + + // �΂ߕ���2 + for (row = 0, check = true; row < BOARD_HEIGHT; row++) { + if (m_Board[row * BOARD_WIDTH + (BOARD_HEIGHT - 1 - row)] * sign[player] <= 0) check = false; + } + if (check) return player; + + // ������ + for (row = 0; row < BOARD_HEIGHT; row++) { + for (col = 0, check = true; col < BOARD_WIDTH; col++) { + if (m_Board[row * BOARD_WIDTH + col] * sign[player] <= 0) check = false; + } + if (check) return player; + } + + // �c���� + for (col = 0; col < BOARD_WIDTH; col++) { + for (row = 0, check = true; row < BOARD_HEIGHT; row++) { + if (m_Board[row * BOARD_WIDTH + col] * sign[player] <= 0) check = false; + } + if (check) return player; + } + } + + return -1; +} + +// �Օ\�� +void T3EBoard::ShowBoard() +{ + for (int i = 0; i < BOARD_SIZE; i++) { + std::cout << (m_Board[i] == 0 ? "�@" : + (m_Board[i] > 0 ? "��" : "�~")); + if (i % BOARD_WIDTH < BOARD_WIDTH - 1) { + std::cout << "��"; + } + else { + std::cout << std::endl; + if (i < BOARD_SIZE - 1) std::cout << "����������" << std::endl; + } + } +} diff --git a/TicTacToeEx/T3EBoard.h b/TicTacToeEx/T3EBoard.h new file mode 100644 index 0000000..d05eecb --- /dev/null +++ b/TicTacToeEx/T3EBoard.h @@ -0,0 +1,31 @@ +#pragma once + +// TicTacToeEx �Q�[���Ǘ��N���X +class T3EBoard +{ + static const int BOARD_WIDTH = 3; // �Ղ̕� + static const int BOARD_HEIGHT = 3; // �Ղ̍��� + static const int BOARD_SIZE = BOARD_WIDTH * BOARD_HEIGHT; // �Ղ̃T�C�Y + static const int MAX_PIECE_ON_BOARD = 3; // �Տ�ɑ��݂ł���R�}�̐� + static const int MAX_TURNS = 99; // �ő�^�[���� + static const int NUM_PLAYERS = 2; // �v���C���[�� + enum { PLAYER1 = 0, PLAYER2 = 1 }; + enum { PLAYER_HUMAN = 0, PLAYER_CPU = 1 }; + enum { NONE = 0, STONE1 = 1, STONE2 = -1 }; + + int m_Board[BOARD_SIZE]; // �Ղ̏�� 0:�Ȃ� ��:���R�} ��:���R�} �l:�u�����^�[���� + int m_Turn; // �^�[�� + int m_NextPlayer; // ���̔� PLAYER1 or PLAYER2 + int m_Player[NUM_PLAYERS]; // �v���C���[ 0:�l�� 1:CPU + + void Reset(); // �Q�[�������� + void ShowBoard(); // �Օ\�� + void SetStone(int place); // �R�}��u�� + int CPU(); // CPU�̎v�l + int Human(); // �l�Ԃ̎�� + int CheckWin(); // �������� +public: + T3EBoard(); // �R���X�g���N�^ + ~T3EBoard(); // �f�X�g���N�^ + void Play(); // �Q�[���i�s +}; diff --git a/TicTacToeEx/TicTacToeEx.vcxproj b/TicTacToeEx/TicTacToeEx.vcxproj new file mode 100644 index 0000000..df77ed8 --- /dev/null +++ b/TicTacToeEx/TicTacToeEx.vcxproj @@ -0,0 +1,158 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + {4AF55B07-0A19-42C8-A704-814E682E44CC} + Win32Proj + TicTacToeEx + 10.0.17134.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + + + Console + true + true + true + + + + + + + + + + + + + \ No newline at end of file diff --git a/TicTacToeEx/TicTacToeEx.vcxproj.filters b/TicTacToeEx/TicTacToeEx.vcxproj.filters new file mode 100644 index 0000000..9635b44 --- /dev/null +++ b/TicTacToeEx/TicTacToeEx.vcxproj.filters @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + ソース ファイル + + + ソース ファイル + + + + + ヘッダー ファイル + + + \ No newline at end of file diff --git a/TicTacToeEx/TicTacToeEx.vcxproj.user b/TicTacToeEx/TicTacToeEx.vcxproj.user new file mode 100644 index 0000000..0b0f24d --- /dev/null +++ b/TicTacToeEx/TicTacToeEx.vcxproj.user @@ -0,0 +1,6 @@ + + + + true + + \ No newline at end of file diff --git a/TicTacToeEx/main.cpp b/TicTacToeEx/main.cpp new file mode 100644 index 0000000..0c4d0be --- /dev/null +++ b/TicTacToeEx/main.cpp @@ -0,0 +1,9 @@ +#include "T3EBoard.h" + +int main() +{ + T3EBoard board; + board.Play(); + + return 0; +} \ No newline at end of file diff --git a/TicTacToeEx/x64/Debug/TicTacToeEx.Build.CppClean.log b/TicTacToeEx/x64/Debug/TicTacToeEx.Build.CppClean.log new file mode 100644 index 0000000..c44e322 --- /dev/null +++ b/TicTacToeEx/x64/Debug/TicTacToeEx.Build.CppClean.log @@ -0,0 +1,13 @@ +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\vc141.pdb +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\vc141.idb +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\t3eboard.obj +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\main.obj +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\x64\debug\tictactoeex.ilk +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\x64\debug\tictactoeex.exe +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\x64\debug\tictactoeex.pdb +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\tictactoeex.tlog\cl.command.1.tlog +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\tictactoeex.tlog\cl.read.1.tlog +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\tictactoeex.tlog\cl.write.1.tlog +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\tictactoeex.tlog\link.command.1.tlog +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\tictactoeex.tlog\link.read.1.tlog +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\x64\debug\tictactoeex.tlog\link.write.1.tlog diff --git a/TicTacToeEx/x64/Debug/TicTacToeEx.log b/TicTacToeEx/x64/Debug/TicTacToeEx.log new file mode 100644 index 0000000..1fb734f --- /dev/null +++ b/TicTacToeEx/x64/Debug/TicTacToeEx.log @@ -0,0 +1,4 @@ + T3EBoard.cpp +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\t3eboard.cpp(72): warning C4101: 'j': ローカル変数は 1 度も使われていません。 +c:\users\sato yukiya\desktop\tictactoeexbase\tictactoeexbase\tictactoeex\t3eboard.cpp(75): warning C4101: 'Me': ローカル変数は 1 度も使われていません。 + TicTacToeEx.vcxproj -> C:\Users\sato yukiya\Desktop\TicTacToeExBase\TicTacToeExBase\x64\Debug\TicTacToeEx.exe