Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace _02.TronRacers
- {
- class Program
- {
- private static int colCount;
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- char[][] matrix = new char[n][];
- int firstPlayerRow = 0;
- int firstPlayerCol = 0;
- bool firstPlayerFound = false;
- int secondPlayerRow = 0;
- int secondPlayerCol = 0;
- bool secondPlayerFound = false;
- int cols = 0;
- for (int row = 0; row < n; row++)
- {
- char[] currentRow = Console.ReadLine().ToCharArray();
- cols = currentRow.Length;
- for (int col = 0; col < currentRow.Length; col++)
- {
- matrix[row] = currentRow;
- char currentSymbol = currentRow[col];
- if(currentSymbol == 'f')
- {
- firstPlayerRow = row;
- firstPlayerCol = col;
- firstPlayerFound = true;
- }
- else if (currentSymbol == 's')
- {
- secondPlayerRow = row;
- secondPlayerCol = col;
- secondPlayerFound = true;
- }
- if(firstPlayerFound && secondPlayerFound)
- {
- break;
- }
- }
- }
- int rowCount = matrix.GetLength(0);
- colCount = cols;
- 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 (RowIndexIsOursideToTheUp(firstPlayerRow,matrix))
- {
- firstPlayerRow = matrix.GetLength(0) - 1;
- }
- else if (RowIndexIsOursideToTheDown(firstPlayerRow, matrix))
- {
- firstPlayerRow = 0;
- }
- else if (ColIndexIsOursideLeft(firstPlayerCol, matrix))
- {
- firstPlayerCol = matrix.GetLength(1) - 1;
- }
- else if (ColIndexIsOursideRight(firstPlayerCol, matrix))
- {
- firstPlayerCol = 0;
- }
- //index validation player 2
- if (RowIndexIsOursideToTheUp(secondPlayerRow, matrix))
- {
- secondPlayerRow = matrix.GetLength(0) - 1;
- }
- else if (RowIndexIsOursideToTheDown(secondPlayerRow, matrix))
- {
- secondPlayerRow = 0;
- }
- else if (ColIndexIsOursideLeft(secondPlayerCol, matrix))
- {
- secondPlayerCol = colCount - 1;
- }
- else if (ColIndexIsOursideRight(secondPlayerCol, matrix))
- {
- secondPlayerCol = 0;
- }
- char currentSymbolPlayer1 = matrix[firstPlayerRow][firstPlayerCol];
- char currentSymbolPlayer2 = matrix[secondPlayerRow][secondPlayerCol];
- if(currentSymbolPlayer1 == 's')
- {
- secondPlayerIsAlive = false;
- matrix[firstPlayerRow][firstPlayerCol] = 'x';
- break;
- }
- else
- {
- matrix[firstPlayerRow][firstPlayerCol] = 'f';
- }
- if(currentSymbolPlayer2 == 'f')
- {
- firstPlayerIsAlive = false;
- matrix[secondPlayerRow][secondPlayerCol] = 'x';
- break;
- }
- else
- {
- matrix[secondPlayerRow][secondPlayerCol] = 's';
- }
- }
- foreach (var row in matrix)
- {
- Console.WriteLine(string.Join("",row));
- }
- }
- public static bool RowIndexIsOursideToTheUp(int row, char[][] matrix)
- {
- return row < 0;
- }
- public static bool RowIndexIsOursideToTheDown(int row, char[][] matrix)
- {
- return row >= matrix.GetLength(0);
- }
- public static bool ColIndexIsOursideLeft(int col, char[][] matrix)
- {
- return col < 0;
- }
- public static bool ColIndexIsOursideRight(int col, char[][] matrix)
- {
- return col >= colCount;
- }
- public static void RowIndexIsOutsideTheMatrix(int row,char[][] matrix)
- {
- if(row < 0)
- {
- row = matrix.GetLength(0);
- }
- else if (row >= matrix.GetLength(0))
- {
- row = 0;
- }
- }
- public static void ColIndexIsOutsideTheMatrix(int col, char[][] matrix)
- {
- if (col < 0)
- {
- col = matrix.GetLength(1);
- }
- else if (col >= matrix.GetLength(1))
- {
- col = 0;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment