Advertisement
AvengersAssemble

TicTacToe new 0.2

Sep 16th, 2013
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.91 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace TicTacToe
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             bool validSelection = false;
  14.             char userType = '.';
  15.             bool userIsX = true;
  16.             int scoreX = 0;
  17.             int scoreO = 0;
  18.             int ties = 0;
  19.             int gameNum = 1;
  20.             char[,] board = new char[3, 3]; //הגדרת מערך לוח
  21.             while (true)
  22.             {
  23.                 SetBoard(board); //קריאה לפונקציית אתחול לוח
  24.                 if (gameNum == 1)
  25.                 {
  26.                     UpdateResult(board, isWinner(board, userType), userType, isTie(board), ref scoreX, ref scoreO, ref gameNum, ref ties); // מעדכן תוצאה
  27.                     Console.WriteLine("\n                                     GAME " + gameNum + ":\n                       Player X: " + scoreX + " | Player O: " + scoreO + " | Ties: " + ties + "\n"); //מעדכן תוצאה
  28.                     Console.WriteLine("                                     BOARD");
  29.                     PrintBoard(board); //קריאה לפונקציה מדפיסת לוח
  30.                     Console.WriteLine("\n\n                          Welcome to Tic Tac Toe v0.7!\n\nThe rules are simple: Two players decide which is X and which is O, please note that X always goes first. In order to win, one must have three in a row, \nhorizontally, vertically or diagonally. If all nine spots are filled, then it \nis a tie, and you will be given the opportunity to play again. Let's begin!"); //הסברת המשחק
  31.                 }
  32.                 else
  33.                 {
  34.                     Console.WriteLine("                                     BOARD");
  35.                     PrintBoard(board); //קריאה לפונקציה מדפיסת לוח
  36.                 }
  37.                 while (!isWinner(board, userType) && !isTie(board))
  38.                 {
  39.                     Console.WriteLine();
  40.                     if (userIsX) //מגדיר שחקן X
  41.                     {
  42.                         Console.BackgroundColor = ConsoleColor.White;
  43.                         Console.ForegroundColor = ConsoleColor.Blue;
  44.                         Console.WriteLine("Player: X");
  45.                         userType = 'X';
  46.                     }
  47.                     else //מגדיר שחקן O
  48.                     {
  49.                         Console.BackgroundColor = ConsoleColor.Blue;
  50.                         Console.ForegroundColor = ConsoleColor.White;
  51.                         Console.WriteLine("Player: O");
  52.                         userType = 'O';
  53.  
  54.                     }
  55.                     validSelection = false;
  56.                     while (!validSelection) //כל עוד לא התבצעה בחירה חוקית
  57.                     {
  58.                         Console.WriteLine("Select row, then press 'enter' (return), then select column."); //הסבר על בחירת מיקום
  59.                         int row = int.Parse(Console.ReadLine()); //משתמש בוחר מיקום
  60.                         int column = int.Parse(Console.ReadLine());
  61.                         if (row > 3 || row < 1 || column > 3 || column < 1) //בודק שהמספר בטווח
  62.                         {
  63.                             Console.WriteLine("Invalid row/column! There are only 3 rows and 3 columns on the board!\nPlease choose again.\n");
  64.                         }
  65.                         else if (board[column - 1, row - 1] == 'X' || board[column - 1, row - 1] == 'O') //בודק אם הבחירה חוקית
  66.                         {
  67.                             Console.WriteLine("You cannot overwrite you/your friend! Please choose again.");
  68.                         }
  69.                         else //מתקיים אם הבחירה תקינה
  70.                         {
  71.                             board[column - 1, row - 1] = userType;
  72.                             validSelection = true;
  73.                         }
  74.                     }
  75.                     userIsX = !userIsX;
  76.                     Console.BackgroundColor = ConsoleColor.Black;
  77.                     Console.ForegroundColor = ConsoleColor.Gray;
  78.                     PrintBoard(board);
  79.                     if (isWinner(board, userType)) //בודק אם הפונקציה שבודקת אם יש מנצח מחזירה true או false
  80.                     {
  81.                         Console.WriteLine("Congratulations! The winner is " + userType + "\n");
  82.                     }
  83.                     if (isTie(board)) //קריאה לפונקציה בודקת תיקו
  84.                     {
  85.                         Console.WriteLine("What a though game! The game resulted in a TIE!\n");
  86.                     }
  87.                 }
  88.  
  89.                 Console.WriteLine("Would you like to play again?\nPress 'enter' to play again, type 'n' then enter to quit."); //מאפשר יציאה מהתוכנית
  90.                 string restart = Console.ReadLine();
  91.                 if (restart == "n" || restart == "N")
  92.                 {
  93.                     break;
  94.                 }
  95.                 UpdateResult(board, isWinner(board, userType), userType, isTie(board), ref scoreX, ref scoreO, ref gameNum, ref ties); //מעדכן תוצאה
  96.                 Console.WriteLine("\n                                     GAME " + gameNum + ":\n                       Player X: " + scoreX + " | Player O: " + scoreO + " | Ties: " + ties + "\n"); //מדפיס תוצאה
  97.             }
  98.         }
  99.         static void SetBoard(char[,] board) //מאתחל לוח
  100.         {
  101.             string strBoard;
  102.             int intBoard;
  103.             for (int i = 0; i < board.GetLength(0); i++)
  104.             {
  105.                 for (int z = 0; z < board.GetLength(1); z++)
  106.                 {
  107.                     intBoard = z + 1;
  108.                     strBoard = Convert.ToString(intBoard);
  109.                     board[z, i] = Convert.ToChar(strBoard);
  110.                     //Console.WriteLine(board[z, i]); בדיקת איתחול
  111.                 }
  112.             }
  113.         }
  114.         static void PrintBoard(char[,] board)//מדפיס לוח
  115.         {
  116.             for (int i = 0; i < board.GetLength(0); i++)
  117.             {
  118.                 Console.Write("\n                                     ");
  119.                 for (int k = 0; k < board.GetLength(1); k++)
  120.                 {
  121.                     Console.Write("{0} ", board[k, i]);
  122.                 }
  123.             }
  124.             Console.WriteLine();
  125.         }
  126.         static bool isWinner(char[,] board, char userType) //בודק אם יש מנצח
  127.         {
  128.             if (board[0, 0] == userType && board[1, 0] == userType && board[2, 0] == userType)
  129.             {
  130.                 return true;
  131.             }
  132.             else if (board[0, 1] == userType && board[1, 1] == userType && board[2, 1] == userType)
  133.             {
  134.                 return true;
  135.             }
  136.             else if (board[0, 2] == userType && board[1, 2] == userType && board[2, 2] == userType)
  137.             {
  138.                 return true;
  139.             }
  140.             else if (board[0, 0] == userType && board[1, 1] == userType && board[2, 2] == userType)
  141.             {
  142.                 return true;
  143.             }
  144.             else if (board[0, 2] == userType && board[1, 1] == userType && board[2, 0] == userType)
  145.             {
  146.                 return true;
  147.             }
  148.             else if (board[0, 0] == userType && board[0, 1] == userType && board[0, 2] == userType)
  149.             {
  150.                 return true;
  151.             }
  152.             else if (board[1, 0] == userType && board[1, 1] == userType && board[1, 2] == userType)
  153.             {
  154.                 return true;
  155.             }
  156.             else if (board[2, 0] == userType && board[2, 1] == userType && board[2, 2] == userType)
  157.             {
  158.                 return true;
  159.             }
  160.             else
  161.             {
  162.                 return false;
  163.             }
  164.         }
  165.         static bool isTie(char[,] board) //בודק אם המשחק הגיע למצב של תיקו
  166.         {
  167.             foreach (char currentChar in board)
  168.             {
  169.                 if (currentChar != 'X' && currentChar != 'O')
  170.                 {
  171.                     return false;
  172.                 }
  173.             }
  174.             return true;
  175.         }
  176.         static void UpdateResult(char[,] board, bool isWinner, char userType, bool isTie, ref int scoreX, ref int scoreO, ref int gameNum, ref int ties) //מעדכן תוצאה
  177.         {
  178.             if (isWinner)
  179.             {
  180.                 if (userType == 'X')
  181.                 {
  182.                     scoreX++;
  183.                 }
  184.                 if (userType == 'O')
  185.                 {
  186.                     scoreO++;
  187.                 }
  188.                 gameNum++;
  189.             }
  190.             if (isTie)
  191.             {
  192.                 ties++;
  193.                 gameNum++;
  194.             }
  195.         }
  196.     }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement