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;
- namespace GameOfLife
- {
- class Program
- {
- public static void UpdateBoard(int[,] arr, int[,] copy)
- {
- for (int i = 1; i < arr.GetLength(0) - 1; i++)
- {
- for (int k = 1; k < arr.GetLength(1) - 1; k++)
- UpdateCell(arr, i, k, copy);
- }
- }
- public static void UpdateCell(int[,] arr, int a, int b, int[,] copy)
- {
- int neihbors = CountNeighbors(arr, a, b);
- switch (neihbors)
- {
- case 0:
- case 1:
- copy[a, b] = 0;
- break;
- case 2:
- break;
- case 3:
- if (arr[a, b] == 0)
- copy[a, b] = 1;
- break;
- default:
- copy[a, b] = 0;
- break;
- }
- }
- public static int CountNeighbors(int[,] arr, int a, int b)
- {
- int count = 0;
- if (arr[a, b] == 1)
- count--;
- for (int i = a - 1; i < a + 2; i++)
- {
- for (int k = b - 1; k < b + 2; k++)
- {
- count += (arr[i, k] + Math.Abs(arr[i, k])) / 2;
- }
- }
- return count;
- }
- public static void InitializeArray(int[,] arr)
- {
- for (int i = 0; i < arr.GetLength(0); i += arr.GetLength(0) - 1)
- for (int k = 0; k < arr.GetLength(1); k++)
- arr[i, k] = -1;
- for (int i = 0; i < arr.GetLength(1); i += arr.GetLength(1) - 1)
- for (int k = 0; k < arr.GetLength(0); k++)
- arr[k, i] = -1;
- for (int i = 1; i < arr.GetLength(0) - 1; i++)
- {
- for (int k = 1; k < arr.GetLength(1) - 1; k++)
- arr[i, k] = int.Parse(Console.ReadLine());
- }
- }
- public static void TestSetup(int[,] arr)
- {
- for (int i = 0; i < arr.GetLength(0); i += arr.GetLength(0) - 1)
- for (int k = 0; k < arr.GetLength(1); k++)
- arr[i, k] = -1;
- for (int i = 0; i < arr.GetLength(1); i += arr.GetLength(1) - 1)
- for (int k = 0; k < arr.GetLength(0); k++)
- arr[k, i] = -1;
- for (int i = 1; i < arr.GetLength(0) - 1; i++)
- {
- for (int k = 1; k < arr.GetLength(1) - 1; k++)
- {
- if (k == i || k == arr.GetLength(1) - i - 1)
- arr[i, k] = 1;
- }
- }
- }
- public static void RandomSetup(int[,] arr)
- {
- Random rnd = new Random();
- for (int i = 0; i < arr.GetLength(0); i += arr.GetLength(0) - 1)
- for (int k = 0; k < arr.GetLength(1); k++)
- arr[i, k] = -1;
- for (int i = 0; i < arr.GetLength(1); i += arr.GetLength(1) - 1)
- for (int k = 0; k < arr.GetLength(0); k++)
- arr[k, i] = -1;
- for (int i = 1; i < arr.GetLength(0) - 1; i++)
- {
- for (int k = 1; k < arr.GetLength(1) - 1; k++)
- {
- arr[i, k] = rnd.Next(0, 2);
- for (int m = 0; m < rnd.Next(1, 3); m++)
- {
- if (arr[i, k] == 1)
- arr[i, k] = rnd.Next(0, 2);
- }
- }
- }
- }
- public static void CopyBoard(int[,] original, int[,] copy)
- {
- for (int i = 0; i < original.GetLength(0); i++)
- {
- for (int k = 0; k < original.GetLength(1); k++)
- copy[i, k] = original[i, k];
- }
- }
- public static void Print(int[,] arr)
- {
- for (int i = 0; i < arr.GetLength(0); i++)
- {
- for (int k = 0; k < arr.GetLength(1); k++)
- {
- switch (arr[i, k])
- {
- case 1:
- Console.Write(((char)46).ToString());
- break;
- case 0:
- Console.Write(" ");
- break;
- default:
- Console.Write(" ");
- break;
- }
- }
- Console.WriteLine();
- }
- }
- public static bool GameOver(int[,] board)
- {
- for (int i = 0; i < board.GetLength(0); i++)
- {
- for (int k = 0; k < board.GetLength(1); k++)
- if (board[i, k] == 1)
- return false;
- }
- return true;
- }
- public static bool StandStill(int[,] board, int[,] lastBoard)
- {
- for (int i = 0; i < board.GetLength(0); i++)
- {
- for (int k = 0; k < board.GetLength(1); k++)
- {
- if (board[i, k] != lastBoard[i, k])
- return false;
- }
- }
- return true;
- }
- static void Main(string[] args)
- {
- char playAgain = 'y';
- int[,] gameBoard = new int[15, 65];
- int[,] boardCopy = new int[gameBoard.GetLength(0), gameBoard.GetLength(1)];
- //InitializeArray(gameBoard);
- //TestSetup(gameBoard);
- while (playAgain.ToString().ToLower().Equals("y"))
- {
- RandomSetup(gameBoard);
- CopyBoard(gameBoard, boardCopy);
- do
- {
- CopyBoard(boardCopy, gameBoard);
- Print(gameBoard);
- UpdateBoard(gameBoard, boardCopy);
- System.Threading.Thread.Sleep(400);
- Console.Clear();
- }
- while (!GameOver(gameBoard) && !StandStill(boardCopy, gameBoard));
- if (GameOver(gameBoard))
- Console.WriteLine("Congratulations, you won!");
- else
- Console.WriteLine("You lose!");
- Console.WriteLine("Play again? y/n");
- playAgain = char.Parse(Console.ReadLine());
- Console.Clear();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement