[Solved] Chessboard Game - C# Task

  

3
Topic starter

Goshko is a keen chess player. One day he was bored with his work and decided to take a break and create a game using the chessboard. He takes a string, e.g. "Software University_2345", converts its symbols to numbers through their ASCII codes and fills a chessboard with them. He takes the values of capital and small letters and digits only. The value of any other symbol is zero. He fills the board’s squares with the numbers, from left to right and from top to bottom (see the example below). The size of the chessboard is n*n (e.g. n = 5) and it always starts with a black squareN will always be an odd number.

chess board

Let’s assume that there are two competing teams: the black team and the white team. Every team’s score is the sum of the values in its squares. However if a square contains a capital letter its value should be given to the opposing team. In the example above the scores are calculated as follows:

White Team Score = 83 'S' + 111 'o' + 116 't' + 97 'a' + 101 'e' + 105 'i' + 101 'e' + 115 's' + 116 't' + 51 '3' + 53 '5' = 1049
Black Team Score = 102 'f' + 119 'w' + 114 'r' + 85 'U' + 110 'n' + 118 'v' + 114 'r' + 105 'i' + 121 'y' + 50 '2' + 52 '4' = 1090.

Input

The input data should be read from the console.

  • The first line holds the size n of the chessboard.
  • The second line holds the input string.

The input data will always be valid and in the format described. There is no need to check it explicitly.

Output

The output should be printed on the console.

  • The first output line holds the winning team in format: “The winner is: {name} team”.
  • The second line holds the difference between the scores of the winning and the losing team.
  • In case the score is equal, print “Equal result: {points}”. Do not print the difference in this case!

Constraints

  • The number n will be an odd integer in the range [1 … 9].
  • Allowed working time for your program: 0.1 seconds. Allowed memory: 16 MB.

Examples

input output examples of the task

1 Answer
2

Very interesting task indeed! A lot of stuff is clearing out in my head when it comes to C# and using different methods to solve those kind of tasks!

How I solved this task? Here's how:

  • First - the input - we'll need int type and then we will multiply it by itself to get the number of the board desk;
  • Then we'll use string for the input - we'll use the string - NOT directly array - because we all know that A PARTICULAR STRING IS AN ARRAY OF CHARACTERS! Very important and useful information!
  • Then - the cellsCount will be the board size (boardSize * boardSize);
  • It is important to remember that we have a particular condition - and it is: cellsCount > inputString.Length  - otherwise we will have an exception error!
  • So we are adding EMPTY additional char with the value of zero 0 (new string(' ', cellsCount - inputString.Length);) as it is stated in the task; After that we are ready for the task itself! 🙂

And the logic for solving the task is:

  • We are creating 2 integers - for calculating the black team's and white team's score. Do not forget that when we are going to add a number - we are using 0; and when we are going to multiply - we use 1 (do NOT mix them!)
  • We need for loop and it's size will be the cellsCount (boardSize * boardSize);
  • In the for loop - we are creating char currentChar which is an element of the inputString[letter] - we are using letter in the square brackets for the number of the inputString; This line: char currentChar = inputString[letter]; in my opinion is crucial!
  • Then we have 3 conditions in order to calculate the sum of the chars (use ASCII table -  https://www.asciitable.com/  - we need to have: ((currentChar >= 'a' && currentChar <= 'z') ||(currentChar >= 'A' && currentChar <= 'Z') ||(currentChar >= '0' && currentChar <= '9'))
  • ...and then 2 conditions for the odd (letter % 2 != 0) or even number (letter % 2 == 0) - to know for which team we are going to add the scores.
  • Then some if/else for the capital letters from the task's explanation.
  • And at the end - just print the results! 🙂

Interesting and really amazing task! thanks, Jolie for sharing it with the community!

Here is the code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
class ChessboardGame
{
    static void Main()
    {
        int boardSize = int.Parse(Console.ReadLine());
        string inputString = Console.ReadLine();
 
        int cellsCount = boardSize * boardSize;
        if (cellsCount > inputString.Length)
        {
            string additionalCharacters = new string(' ', cellsCount - inputString.Length);
            inputString = inputString + additionalCharacters;
        }
 
        int blackTeamScore = 0;
        int whiteTeamScore = 0;
 
        for (int letter = 0; letter < cellsCount; letter++)
        {
            char currentChar = inputString[letter];
 
            if ((currentChar >= 'a' && currentChar <= 'z') ||
                (currentChar >= 'A' && currentChar <= 'Z') ||
                (currentChar >= '0' && currentChar <= '9'))
            {
                if (letter % 2 == 0)
                {
                    if (currentChar >= 'A' && currentChar <= 'Z')
                    {
                        whiteTeamScore += currentChar;
                    }
                    else
                    {
                        blackTeamScore += currentChar;
                    }
                }
                else if (letter % 2 != 0)
                {
                    if (currentChar >= 'A' && currentChar <= 'Z')
                    {
                        blackTeamScore += currentChar;
                    }
                    else
                    {
                        whiteTeamScore += currentChar;
                    }
                }
            }
        }
        if (blackTeamScore > whiteTeamScore)
        {
            Console.WriteLine("The winner is: black team");
            Console.WriteLine(blackTeamScore - whiteTeamScore);
        }
        if (whiteTeamScore > blackTeamScore)
        {
            Console.WriteLine("The winner is: white team");
            Console.WriteLine(whiteTeamScore - blackTeamScore);
        }
        else if (blackTeamScore == whiteTeamScore)
        {
            Console.WriteLine("Equal result: {0}", whiteTeamScore);
        }
    }
}
Share: