Advertisement
AvengersAssemble

Tic Tac Toe v0.7 /w score system

Sep 6th, 2013
612
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 8.16 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 cheating=true;
  14.             char userType='.';
  15.             int scoreX = 0;
  16.             int scoreO = 0;
  17.             int ties = 0;
  18.             int isWinner = 0;
  19.             int gameNum = 1;
  20.             char[,] board = new char[3, 3]; //הגדרת מערך לוח
  21.             while (true)
  22.             {
  23.                 bool tie = isTie(board);
  24.                 SetBoard(board); //קריאה לפונקציית אתחול לוח
  25.                 Result(board, isWinner, userType, tie, ref scoreX, ref scoreO, ref gameNum, ref ties);
  26.                 Console.WriteLine("                                     BOARD");
  27.                 PrintBoard(board); //קריאה לפונקציה מדפיסת לוח
  28.                 if (gameNum == 1)
  29.                     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!"); //הסברת המשחק
  30.                 for (int k = 1; k > 0; k++)
  31.                 {
  32.                     Console.WriteLine();
  33.                     if (k % 2 == 0) //מגדיר שחקן O
  34.                     {
  35.                         Console.BackgroundColor = ConsoleColor.Blue;
  36.                         Console.ForegroundColor = ConsoleColor.White;
  37.                         Console.WriteLine("Player: O");
  38.                         userType = 'O';
  39.  
  40.                     }
  41.                     else //מגדיר שחקן X
  42.                     {
  43.                         Console.BackgroundColor = ConsoleColor.White;
  44.                         Console.ForegroundColor = ConsoleColor.Blue;
  45.                         Console.WriteLine("Player: X");
  46.                         userType = 'X';
  47.                     }
  48.                     while (cheating) //לולאה שמתקיימת אם נמצאת רמאות
  49.                     {
  50.                         Console.WriteLine("Select row, then press 'enter' (return), then select column."); //הסבר על בחירת מיקום
  51.                         int row = int.Parse(Console.ReadLine()); //משתמש בוחר מיקום
  52.                         int column = int.Parse(Console.ReadLine());
  53.                         if (row > 3 || row < 1 || column > 3 || column < 1) //בודק שהמספר בטווח
  54.                             Console.WriteLine("Invalid row/column! There are only 3 rows and 3 columns on the board!\nPlease choose again.\n");
  55.                         else if (board[column - 1, row - 1] == 'X' || board[column - 1, row - 1] =='O') //בודק רמאות
  56.                             Console.WriteLine("You cannot overwrite you/your friend! Please choose again.");
  57.                         else //מתקיים אם הבחירה תקינה
  58.                         {
  59.                             board[column - 1, row - 1] = userType;
  60.                             cheating = false;
  61.                         }
  62.                     }
  63.                     Console.BackgroundColor = ConsoleColor.Black;
  64.                     Console.ForegroundColor = ConsoleColor.Gray;
  65.                     PrintBoard(board);
  66.                     cheating = true;
  67.                     isWinner = Winner(board, userType); //קריאה לפונקציה בודקת מנצח
  68.                     if (isWinner == 1) // מתקיים אם יש מנצח = אם הפונקציה מחזירה את הערך 1
  69.                     {
  70.                         Console.WriteLine("Congratulations! The winner is " + userType + "\n");
  71.                         break; //מסיים את לולאת ה-for
  72.                     }
  73.                     if (isTie(board)) //קריאה לפונקציה בודקת תיקו
  74.                     {
  75.                         Console.WriteLine("What a though game! The game resulted in a TIE!\n");
  76.                         break; //אם יש תיקו - מסיים לולאת for
  77.                     }
  78.                 }
  79.                 Console.WriteLine("Would you like to play again?\nPress 'enter' to play again, type 'n' then enter to quit."); //מאפשר יציאה מהתוכנית
  80.                 string restart = Console.ReadLine();
  81.                 if (restart == "n" || restart == "N")
  82.                     break;
  83.             }
  84.         }
  85.         static void SetBoard(char[,] board) //מאתחל לוח
  86.         {
  87.             string strBoard;
  88.             int intBoard;
  89.             for (int i = 0; i < board.GetLength(0); i++)
  90.             {
  91.                 for (int z = 0; z < board.GetLength(1); z++)
  92.                 {
  93.                     intBoard = z + 1;
  94.                     strBoard = Convert.ToString(intBoard);
  95.                     board[z, i] = Convert.ToChar(strBoard);
  96.                     //Console.WriteLine(board[z, i]); בדיקת איתחול
  97.                 }
  98.             }
  99.         }
  100.         static void PrintBoard(char[,] board)//מדפיס לוח
  101.         {
  102.             for (int i = 0; i < board.Length; i++)
  103.             {
  104.                 if (i == 0 || i == 3 || i == 6)
  105.                     Console.Write("\n                                     ");
  106.                 if (i < 3)
  107.                     Console.Write(board[i, 0] + " ");
  108.                 //if (i == 3 || i == 6)
  109.                   //  Console.WriteLine();
  110.                 if (i >= 3 && i < 6)
  111.                     Console.Write(board[i - 3, 1] + " ");
  112.                 if (i >= 6)
  113.                     Console.Write(board[i - 6, 2] + " ");
  114.             }
  115.             Console.WriteLine();
  116.         }
  117.         static int Winner(char[,] board, char userType) //בודק אם יש מנצח
  118.         {
  119.             if (board[0, 0] == userType && board[1, 0] == userType && board[2, 0] == userType)
  120.                 return 1;
  121.             else if (board[0, 1] == userType && board[1, 1] == userType && board[2, 1] == userType)
  122.                 return 1;
  123.             else if (board[0, 2] == userType && board[1, 2] == userType && board[2, 2] == userType)
  124.                 return 1;
  125.             else if (board[0, 0] == userType && board[1, 1] == userType && board[2, 2] == userType)
  126.                 return 1;
  127.             else if (board[0, 2] == userType && board[1, 1] == userType && board[2, 0] == userType)
  128.                 return 1;
  129.             else if (board[0, 0] == userType && board[0, 1] == userType && board[0, 2] == userType)
  130.                 return 1;
  131.             else if (board[1, 0] == userType && board[1, 1] == userType && board[1, 2] == userType)
  132.                 return 1;
  133.             else if (board[2, 0] == userType && board[2, 1] == userType && board[2, 2] == userType)
  134.                 return 1;
  135.             else
  136.                 return 0;
  137.         }
  138.         static bool isTie(char[,] board) //בודק אם המשחק הגיע למצב של תיקו
  139.         {
  140.             bool notTie=false;
  141.             foreach (char currentChar in board)
  142.             {
  143.                 if (currentChar != 'X' && currentChar != 'O')
  144.                 {
  145.                     notTie = true;
  146.                     break;
  147.                 }
  148.             }
  149.             return !notTie;
  150.         }
  151.         static void Result(char[,] board, int isWinner, char userType, bool tie, ref int scoreX, ref int scoreO, ref int gameNum, ref int ties) //פונקציית תוצאה
  152.         {
  153.             if (isWinner == 1)
  154.             {
  155.                 if(userType == 'X')
  156.                     scoreX ++;
  157.                 if(userType == 'O')
  158.                     scoreO++;
  159.                 gameNum++;
  160.             }
  161.             if (tie)
  162.             {
  163.                 ties++;
  164.                 gameNum++;
  165.                 Console.WriteLine("TIE");
  166.             }
  167.             Console.WriteLine("\n                                     GAME " + gameNum + ":\n                       Player X: " + scoreX + " | Player O: " + scoreO + " | Ties: " +ties + "\n");
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement