From 4663a8e550b3f9c98b23d29d89debfe1645fd84e Mon Sep 17 00:00:00 2001 From: masykur Date: Fri, 4 Mar 2022 16:31:01 +0700 Subject: [PATCH] Add tictactoe2 in C# and reorganize project folder --- 89_Tic-Tac-Toe/csharp/TicTacToe.sln | 25 +- .../csharp/{ => tictactoe1}/Program.cs | 1 - .../tictactoe1.csproj} | 0 89_Tic-Tac-Toe/csharp/tictactoe2/Program.cs | 232 ++++++++++++++++++ .../csharp/tictactoe2/tictactoe2.csproj | 10 + 89_Tic-Tac-Toe/go/src/tictactoe1.go | 3 +- 6 files changed, 257 insertions(+), 14 deletions(-) rename 89_Tic-Tac-Toe/csharp/{ => tictactoe1}/Program.cs (98%) rename 89_Tic-Tac-Toe/csharp/{TicTacToe.csproj => tictactoe1/tictactoe1.csproj} (100%) create mode 100644 89_Tic-Tac-Toe/csharp/tictactoe2/Program.cs create mode 100644 89_Tic-Tac-Toe/csharp/tictactoe2/tictactoe2.csproj diff --git a/89_Tic-Tac-Toe/csharp/TicTacToe.sln b/89_Tic-Tac-Toe/csharp/TicTacToe.sln index f334433a..a77baf98 100644 --- a/89_Tic-Tac-Toe/csharp/TicTacToe.sln +++ b/89_Tic-Tac-Toe/csharp/TicTacToe.sln @@ -1,25 +1,28 @@  Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio Version 17 -VisualStudioVersion = 17.0.32014.148 +# Visual Studio Version 16 +VisualStudioVersion = 16.0.30114.105 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TicTacToe", "TicTacToe.csproj", "{A318881A-DA1A-499C-8820-69A1BF02B824}" +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tictactoe1", "tictactoe1\tictactoe1.csproj", "{E0AF55BF-4C2B-41C6-B556-E8EC8C1BE647}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "tictactoe2", "tictactoe2\tictactoe2.csproj", "{B98682C9-132E-44F1-B288-8BE04CF53BCF}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {A318881A-DA1A-499C-8820-69A1BF02B824}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {A318881A-DA1A-499C-8820-69A1BF02B824}.Debug|Any CPU.Build.0 = Debug|Any CPU - {A318881A-DA1A-499C-8820-69A1BF02B824}.Release|Any CPU.ActiveCfg = Release|Any CPU - {A318881A-DA1A-499C-8820-69A1BF02B824}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection - GlobalSection(ExtensibilityGlobals) = postSolution - SolutionGuid = {B5343227-EE3B-4829-AC09-DF6702D24501} + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E0AF55BF-4C2B-41C6-B556-E8EC8C1BE647}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E0AF55BF-4C2B-41C6-B556-E8EC8C1BE647}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E0AF55BF-4C2B-41C6-B556-E8EC8C1BE647}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E0AF55BF-4C2B-41C6-B556-E8EC8C1BE647}.Release|Any CPU.Build.0 = Release|Any CPU + {B98682C9-132E-44F1-B288-8BE04CF53BCF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B98682C9-132E-44F1-B288-8BE04CF53BCF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B98682C9-132E-44F1-B288-8BE04CF53BCF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B98682C9-132E-44F1-B288-8BE04CF53BCF}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/89_Tic-Tac-Toe/csharp/Program.cs b/89_Tic-Tac-Toe/csharp/tictactoe1/Program.cs similarity index 98% rename from 89_Tic-Tac-Toe/csharp/Program.cs rename to 89_Tic-Tac-Toe/csharp/tictactoe1/Program.cs index d4955a40..72b98570 100644 --- a/89_Tic-Tac-Toe/csharp/Program.cs +++ b/89_Tic-Tac-Toe/csharp/tictactoe1/Program.cs @@ -1,5 +1,4 @@ // See https://aka.ms/new-console-template for more information -Console.WriteLine("Hello, World!"); // Print text on the screen with 30 spaces before text Console.WriteLine("TIC TAC TOE".PadLeft(30)); // Print text on screen with 15 spaces before text diff --git a/89_Tic-Tac-Toe/csharp/TicTacToe.csproj b/89_Tic-Tac-Toe/csharp/tictactoe1/tictactoe1.csproj similarity index 100% rename from 89_Tic-Tac-Toe/csharp/TicTacToe.csproj rename to 89_Tic-Tac-Toe/csharp/tictactoe1/tictactoe1.csproj diff --git a/89_Tic-Tac-Toe/csharp/tictactoe2/Program.cs b/89_Tic-Tac-Toe/csharp/tictactoe2/Program.cs new file mode 100644 index 00000000..4bfe888f --- /dev/null +++ b/89_Tic-Tac-Toe/csharp/tictactoe2/Program.cs @@ -0,0 +1,232 @@ +// See https://aka.ms/new-console-template for more information +char[] board = new char[10]; +char human; +char computer; +int move = 0; +char result; +for(;;){ + // Print text on the screen with 30 spaces before text + Console.WriteLine("TIC TAC TOE".PadLeft(30)); + // Print text on screen with 15 spaces before text + Console.WriteLine("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY".PadLeft(15)); + // THIS PROGRAM PLAYS TIC TAC TOE + Console.WriteLine("THE BOARD IS NUMBERED:"); + Console.WriteLine("1 2 3"); + Console.WriteLine("4 5 6"); + Console.WriteLine("7 8 9"); + Console.Write("\n\nDO YOU WANT 'X' OR 'O'"); + var key = Console.ReadKey(); + Console.WriteLine(); + // cleanup the board + for(int i=0; i < 10; i++) { + board[i]=' '; + } + // X GOES FIRST + if (key.Key == ConsoleKey.X) { + human = 'X'; + computer = 'O'; + move = readYourMove(); + board[move] = human; + printBoard(); + } else { + human = 'O'; + computer = 'X'; + } + for(;;){ + Console.WriteLine("THE COMPUTER MOVES TO..."); + move = computerMove(move); + board[move] = computer; + result = printBoard(); + printResult(result); + move = readYourMove(); + board[move] = human; + result = printBoard(); + printResult(result); + } +} + +void printResult(int result) { + if (result == '\0') { + Console.WriteLine("IT'S A DRAW. THANK YOU."); + Environment.Exit(0); + } else if (result == computer) { + Console.WriteLine("I WIN, TURKEY!!!"); + Environment.Exit(0); + } else if (result == human) { + Console.WriteLine("YOU BEAT ME!! GOOD GAME."); + Environment.Exit(0); + } +} +char printBoard() { + for (int i=1; i < 10; i++){ + Console.Write($" {board[i]} "); + if (i % 3 == 0) { + if (i < 9) { + Console.Write("\n---+---+---\n"); + } else { + Console.Write("\n"); + } + } else { + Console.Write("!"); + } + } + // horizontal check + for (int i = 1; i <= 9; i += 3) { + if (board[i] != ' ' && (board[i] == board[i+1]) && (board[i+1] == board[i+2])) { + return board[i]; + } + } + // vertical check + for (int i = 1; i <= 3; i++) { + if (board[i] != ' ' && (board[i] == board[i+3]) && (board[i] == board[i+6])) { + return board[i]; + } + } + // cross + if (board[5] != ' ') { + if ((board[1] == board[5] && board[9] == board[5]) || (board[3] == board[5] && board[7] == board[5])) { + return board[5]; + } + } + // draw check + for (int i = 1; i <= 9; i++) { + if (board[i] == ' ') { + return ' '; + } + } + return '\0'; +} + +int readYourMove() { + int number = 0; + for(;;) { + Console.Write("\n\nWHERE DO YOU MOVE? "); + var key = Console.ReadKey(); + Console.WriteLine(); + if (key.Key == ConsoleKey.D0) { + Console.WriteLine("THANKS FOR THE GAME."); + Environment.Exit(0); + } + if (key.Key >= ConsoleKey.D1 && key.Key <= ConsoleKey.D9) { + number = key.Key - ConsoleKey.D0; + if (number > 9 || board[number] != ' ') { + Console.WriteLine("THAT SQUARE IS OCCUPIED.\n"); + continue; + } + } + return number; + } +} + +int getIndex(int number) { + return ((number - 1) % 8) + 1; //number - 8 * (int)((number - 1) / 8); +} +int computerMove(int lastMove) { + int[] boardMap = new int[] {0, 1, 2, 3, 6, 9, 8, 7, 4, 5}; + int index = Array.IndexOf(boardMap, lastMove); + if (lastMove == 0 || board[5] == ' '){ + return 5; + } + if (lastMove == 5) { + return 1; + } + if (board[5] == human) { + // check possible win + if (board[1] == computer && board[2] == ' ' && board[3] == computer) { + return 2; + } + if (board[7] == computer && board[8] == ' ' && board[9] == computer) { + return 8; + } + if (board[1] == computer && board[4] == ' ' && board[7] == computer) { + return 4; + } + if (board[3] == computer && board[6] == ' ' && board[7] == computer) { + return 6; + } + // check cross + int crossIndex = boardMap[getIndex(index + 4)]; + if (board[crossIndex] == ' ') { + return crossIndex; + } + int stepForward2 = boardMap[getIndex(index + 2)]; + if (board[stepForward2] == ' ') { + return stepForward2; + } + int stepBackward2 = boardMap[getIndex(index + 6)]; + if (board[stepBackward2] == ' ') { + return stepBackward2; + } + int stepForward1 = boardMap[getIndex(index + 1)]; + if (board[stepForward1] == ' ') { + return stepForward1; + } + int stepBackward1 = boardMap[getIndex(index + 7)]; + if (board[stepBackward1] == ' ') { + return stepBackward1; + } + int stepForward3 = boardMap[getIndex(index + 3)]; + if (board[stepForward3] == ' ') { + return stepForward3; + } + int stepBackward3 = boardMap[getIndex(index + 5)]; + if (board[stepBackward3] == ' ') { + return stepBackward3; + } + } else { + // check possible win + if (board[1] == computer && board[9] == ' ') { + return 9; + } + if (board[9] == computer && board[1] == ' ') { + return 1; + } + if (board[3] == computer && board[7] == ' ') { + return 7; + } + if (board[7] == computer && board[3] == ' ') { + return 3; + } + // if corner + if (index % 2 == 1) { + int stepForward2 = boardMap[getIndex(index + 2)]; + if (board[stepForward2] == ' ') { + return stepForward2; + } + int stepBackward2 = boardMap[getIndex(index + 6)]; + if (board[stepBackward2] == ' ') { + return stepBackward2; + } + } else { + int stepForward1 = boardMap[getIndex(index + 1)]; + if (board[stepForward1] == ' ') { + return stepForward1; + } + int stepBackward1 = boardMap[getIndex(index + 7)]; + if (board[stepBackward1] == ' ') { + return stepBackward1; + } + int stepForward3 = boardMap[getIndex(index + 3)]; + if (board[stepForward3] == ' ') { + return stepForward3; + } + int stepBackward3 = boardMap[getIndex(index + 5)]; + if (board[stepBackward3] == ' ') { + return stepBackward3; + } + int crossIndex = boardMap[getIndex(index + 4)]; + if (board[crossIndex] == ' ') { + return crossIndex; + } + int stepForward2 = boardMap[getIndex(index + 2)]; + if (board[stepForward2] == ' ') { + return stepForward2; + } + int stepBackward2 = boardMap[getIndex(index + 6)]; + if (board[stepBackward2] == ' ') { + return stepBackward2; + } + } + } + return 0; +} diff --git a/89_Tic-Tac-Toe/csharp/tictactoe2/tictactoe2.csproj b/89_Tic-Tac-Toe/csharp/tictactoe2/tictactoe2.csproj new file mode 100644 index 00000000..74abf5c9 --- /dev/null +++ b/89_Tic-Tac-Toe/csharp/tictactoe2/tictactoe2.csproj @@ -0,0 +1,10 @@ + + + + Exe + net6.0 + enable + enable + + + diff --git a/89_Tic-Tac-Toe/go/src/tictactoe1.go b/89_Tic-Tac-Toe/go/src/tictactoe1.go index a6dcbb69..416830fe 100644 --- a/89_Tic-Tac-Toe/go/src/tictactoe1.go +++ b/89_Tic-Tac-Toe/go/src/tictactoe1.go @@ -6,14 +6,12 @@ import ( ) func main() { - fmt.Printf("Hello, World!\n") // Print text on the screen with 30 spaces before text fmt.Printf("%30s\n", "TIC TAC TOE") // Print text on screen with 15 spaces before text // And print three lines break on screen fmt.Printf("%15s\n\n\n\n", "CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") // THIS PROGRAM PLAYS TIC TAC TOE - // THE MACHINE GOES FIRST fmt.Printf("THE GAME BOARD IS NUMBERED:\n\n") fmt.Println("1 2 3") fmt.Println("8 9 4") @@ -27,6 +25,7 @@ func main() { ) a = 9 fmt.Printf("\n\n") + // THE MACHINE GOES FIRST computerMoves(a) p = readYourMove() b = move(p + 1)