Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _02.TronRacers
- {
- class Program
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[,] matrix = new char[n, n];
- int firstPlayerRow = 0;
- int firstPlayerCol = 0;
- int secondPlayerRow = 0;
- int secondPlayerCol = 0;
- for (int row = 0; row < n; row++)
- {
- char[] currentRow = Console.ReadLine().ToCharArray();
- for (int col = 0; col < n; col++)
- {
- matrix[row, col] = currentRow[col];
- char currentSymbol = matrix[row, col];
- if (currentSymbol == 'f')
- {
- firstPlayerRow = row;
- firstPlayerCol = col;
- }
- else if (currentSymbol == 's')
- {
- secondPlayerRow = row;
- secondPlayerCol = col;
- }
- }
- }
- bool firstPlayerIsAlive = true;
- bool secondPlayerIsAlive = true;
- while (firstPlayerIsAlive && secondPlayerIsAlive)
- {
- string[] input = Console.ReadLine().Split();
- string command1 = input[0];
- string command2 = input[1];
- //first player commands
- if (command1 == "up")
- {
- firstPlayerRow--;
- }
- else if (command1 == "down")
- {
- firstPlayerRow++;
- }
- else if (command1 == "left")
- {
- firstPlayerCol--;
- }
- else if (command1 == "right")
- {
- firstPlayerCol++;
- }
- //second player commands
- if (command2 == "up")
- {
- secondPlayerRow--;
- }
- else if (command2 == "down")
- {
- secondPlayerRow++;
- }
- else if (command2 == "left")
- {
- secondPlayerCol--;
- }
- else if (command2 == "right")
- {
- secondPlayerCol++;
- }
- //index validation player 1
- if (RowIndexIsOursideUp(firstPlayerRow))
- {
- firstPlayerRow = n - 1;
- }
- else if (RowIndexIsOursideDown(firstPlayerRow, n))
- {
- firstPlayerRow = 0;
- }
- else if (ColIndexIsOursideLeft(firstPlayerCol))
- {
- firstPlayerCol = n - 1;
- }
- else if (ColIndexIsOursideRight(firstPlayerCol, n))
- {
- firstPlayerCol = 0;
- }
- //index validation player 2
- if (RowIndexIsOursideUp(secondPlayerRow))
- {
- secondPlayerRow = n - 1;
- }
- else if (RowIndexIsOursideDown(secondPlayerRow, n))
- {
- secondPlayerRow = 0;
- }
- else if (ColIndexIsOursideLeft(secondPlayerCol))
- {
- secondPlayerCol = n - 1;
- }
- else if (ColIndexIsOursideRight(secondPlayerCol, n))
- {
- secondPlayerCol = 0;
- }
- char currentSymbolPlayer1 = matrix[firstPlayerRow, firstPlayerCol];
- char currentSymbolPlayer2 = matrix[secondPlayerRow, secondPlayerCol];
- if (currentSymbolPlayer1 == 's')
- {
- matrix[firstPlayerRow, firstPlayerCol] = 'x';
- break;
- }
- else
- {
- matrix[firstPlayerRow, firstPlayerCol] = 'f';
- }
- if (currentSymbolPlayer2 == 'f')
- {
- matrix[secondPlayerRow, secondPlayerCol] = 'x';
- break;
- }
- else
- {
- matrix[secondPlayerRow, secondPlayerCol] = 's';
- }
- }
- for (int row = 0; row < n; row++)
- {
- for (int col = 0; col < n; col++)
- {
- Console.Write(matrix[row, col]);
- }
- Console.WriteLine();
- }
- }
- public static bool RowIndexIsOursideUp(int row)
- {
- return row < 0;
- }
- public static bool RowIndexIsOursideDown(int row, int n)
- {
- return row >= n;
- }
- public static bool ColIndexIsOursideLeft(int col)
- {
- return col < 0;
- }
- public static bool ColIndexIsOursideRight(int col, int n)
- {
- return col >= n;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment