Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TicTacToe
- {
- class Program
- {
- static void Main(string[] args)
- {
- bool cheating=true;
- char userType='.';
- int scoreX = 0;
- int scoreO = 0;
- int ties = 0;
- int isWinner = 0;
- int gameNum = 1;
- char[,] board = new char[3, 3]; //הגדרת מערך לוח
- while (true)
- {
- bool tie = isTie(board);
- SetBoard(board); //קריאה לפונקציית אתחול לוח
- Result(board, isWinner, userType, tie, ref scoreX, ref scoreO, ref gameNum, ref ties);
- Console.WriteLine(" BOARD");
- PrintBoard(board); //קריאה לפונקציה מדפיסת לוח
- if (gameNum == 1)
- 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!"); //הסברת המשחק
- for (int k = 1; k > 0; k++)
- {
- Console.WriteLine();
- if (k % 2 == 0) //מגדיר שחקן O
- {
- Console.BackgroundColor = ConsoleColor.Blue;
- Console.ForegroundColor = ConsoleColor.White;
- Console.WriteLine("Player: O");
- userType = 'O';
- }
- else //מגדיר שחקן X
- {
- Console.BackgroundColor = ConsoleColor.White;
- Console.ForegroundColor = ConsoleColor.Blue;
- Console.WriteLine("Player: X");
- userType = 'X';
- }
- while (cheating) //לולאה שמתקיימת אם נמצאת רמאות
- {
- Console.WriteLine("Select row, then press 'enter' (return), then select column."); //הסבר על בחירת מיקום
- int row = int.Parse(Console.ReadLine()); //משתמש בוחר מיקום
- int column = int.Parse(Console.ReadLine());
- if (row > 3 || row < 1 || column > 3 || column < 1) //בודק שהמספר בטווח
- Console.WriteLine("Invalid row/column! There are only 3 rows and 3 columns on the board!\nPlease choose again.\n");
- else if (board[column - 1, row - 1] == 'X' || board[column - 1, row - 1] =='O') //בודק רמאות
- Console.WriteLine("You cannot overwrite you/your friend! Please choose again.");
- else //מתקיים אם הבחירה תקינה
- {
- board[column - 1, row - 1] = userType;
- cheating = false;
- }
- }
- Console.BackgroundColor = ConsoleColor.Black;
- Console.ForegroundColor = ConsoleColor.Gray;
- PrintBoard(board);
- cheating = true;
- isWinner = Winner(board, userType); //קריאה לפונקציה בודקת מנצח
- if (isWinner == 1) // מתקיים אם יש מנצח = אם הפונקציה מחזירה את הערך 1
- {
- Console.WriteLine("Congratulations! The winner is " + userType + "\n");
- break; //מסיים את לולאת ה-for
- }
- if (isTie(board)) //קריאה לפונקציה בודקת תיקו
- {
- Console.WriteLine("What a though game! The game resulted in a TIE!\n");
- break; //אם יש תיקו - מסיים לולאת for
- }
- }
- Console.WriteLine("Would you like to play again?\nPress 'enter' to play again, type 'n' then enter to quit."); //מאפשר יציאה מהתוכנית
- string restart = Console.ReadLine();
- if (restart == "n" || restart == "N")
- break;
- }
- }
- static void SetBoard(char[,] board) //מאתחל לוח
- {
- string strBoard;
- int intBoard;
- for (int i = 0; i < board.GetLength(0); i++)
- {
- for (int z = 0; z < board.GetLength(1); z++)
- {
- intBoard = z + 1;
- strBoard = Convert.ToString(intBoard);
- board[z, i] = Convert.ToChar(strBoard);
- //Console.WriteLine(board[z, i]); בדיקת איתחול
- }
- }
- }
- static void PrintBoard(char[,] board)//מדפיס לוח
- {
- for (int i = 0; i < board.Length; i++)
- {
- if (i == 0 || i == 3 || i == 6)
- Console.Write("\n ");
- if (i < 3)
- Console.Write(board[i, 0] + " ");
- //if (i == 3 || i == 6)
- // Console.WriteLine();
- if (i >= 3 && i < 6)
- Console.Write(board[i - 3, 1] + " ");
- if (i >= 6)
- Console.Write(board[i - 6, 2] + " ");
- }
- Console.WriteLine();
- }
- static int Winner(char[,] board, char userType) //בודק אם יש מנצח
- {
- if (board[0, 0] == userType && board[1, 0] == userType && board[2, 0] == userType)
- return 1;
- else if (board[0, 1] == userType && board[1, 1] == userType && board[2, 1] == userType)
- return 1;
- else if (board[0, 2] == userType && board[1, 2] == userType && board[2, 2] == userType)
- return 1;
- else if (board[0, 0] == userType && board[1, 1] == userType && board[2, 2] == userType)
- return 1;
- else if (board[0, 2] == userType && board[1, 1] == userType && board[2, 0] == userType)
- return 1;
- else if (board[0, 0] == userType && board[0, 1] == userType && board[0, 2] == userType)
- return 1;
- else if (board[1, 0] == userType && board[1, 1] == userType && board[1, 2] == userType)
- return 1;
- else if (board[2, 0] == userType && board[2, 1] == userType && board[2, 2] == userType)
- return 1;
- else
- return 0;
- }
- static bool isTie(char[,] board) //בודק אם המשחק הגיע למצב של תיקו
- {
- bool notTie=false;
- foreach (char currentChar in board)
- {
- if (currentChar != 'X' && currentChar != 'O')
- {
- notTie = true;
- break;
- }
- }
- return !notTie;
- }
- static void Result(char[,] board, int isWinner, char userType, bool tie, ref int scoreX, ref int scoreO, ref int gameNum, ref int ties) //פונקציית תוצאה
- {
- if (isWinner == 1)
- {
- if(userType == 'X')
- scoreX ++;
- if(userType == 'O')
- scoreO++;
- gameNum++;
- }
- if (tie)
- {
- ties++;
- gameNum++;
- Console.WriteLine("TIE");
- }
- Console.WriteLine("\n GAME " + gameNum + ":\n Player X: " + scoreX + " | Player O: " + scoreO + " | Ties: " +ties + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement