棋盘游戏练习c#



问题2–棋盘游戏

戈什科是个热衷下棋的人。一天,他对自己的工作感到厌倦,决定休息一下,用棋盘创造一个游戏。他取一个字符串,例如"软件大学_2345",通过ASCII码将其符号转换为数字,并用它们填充棋盘。他只取大写字母、小写字母和数字的值。任何其他符号的值都为零。他用从左到右、从上到下的数字填充棋盘的正方形(见下面的例子(。棋盘的大小是n*n(例如n=5(,并且总是以黑色正方形开始。N永远是一个奇数。

S   o   f   t   w       
a   r   e       U       
n   i   v   e   r       
s   i   t   y   _       
2   3   4   5       

83  111 102 116 119
97  114 101 0   85
110 105 118 101 114
115 105 116 121 0
50  51  52  53  0

假设有两支队伍在竞争:黑人队和白人队。每支球队的得分都是其方格中数值的总和。然而,如果一个正方形包含一个大写字母,那么它的值应该给对方球队。在上面的例子中,分数计算如下:

白队得分=83'S'+111'o'+116't'+97'a'+101'e'+105'i'+101'o'+115'S'+116't'+51'3'+53'5'=1049

黑队得分=102'f'+119'w'+114'r'+85'U'+110'n'+118'v'+114'r'+105'i'+121'y'+50'2'+52'4'=1090。

输入应从控制台读取输入数据。

•第一行包含棋盘的大小n。

•第二行保存输入字符串。

输入数据将始终有效,并采用所描述的格式。没有必要明确检查它。

输出输出应打印在控制台上。

•第一行输出的是获胜队伍的格式:"获胜者是:{name}队伍"。

•第二行是获胜球队和失败球队的得分之差。

•如果分数相等,则打印"相等结果:{分}"。在这种情况下,不要打印差异!

这是我当前的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ChessboardGame
{
    class Program
    {
        static void Main(string[] args)
        {
            int boardSize = int.Parse(Console.ReadLine());
            string inputString = Console.ReadLine();
            char[,] board = new char[boardSize, boardSize];
            int p = inputString.Length;                                         //check so it doesnt go out of range of the string
            int i = 0;                                                          //to know which element of the string to store into the matrix
            int whiteScore = 0;
            int blackScore = 0;
            int oddOrEven = 0;                                                  //to know if the cell is odd or even
            for (int row = 0; row < boardSize; row++)                           //filling the matrix
            {
                if (p == 0)
                    break;
                for (int col = 0; col < boardSize; col++)
                {
                    if (p == 0)
                        break;
                    board[row, col] = inputString[i];
                    i++;
                    p--;
                }
            }                                                                  
            for (int row = 0; row < boardSize; row++)                           //calculating score
            {
                for (int col = 0; col < boardSize; col++)
                {
                    if (oddOrEven % 2 == 0)
                    {
                        if (!(((int)board[row, col] >= 65 && (int)board[row, col] <= 90) && ((int)board[row, col] >= 97 && (int)board[row, col] <= 122)))
                        {
                            if ((int)board[row, col] >= 65 && (int)board[row, col] <= 90)
                            {
                                whiteScore += (int)board[row, col];
                            }
                            else
                            {
                                blackScore += (int)board[row, col];
                            }
                        }
                    }
                    else
                    {
                        if (!(((int)board[row, col] >= 65 && (int)board[row, col] <= 90) && ((int)board[row, col] >= 97 && (int)board[row, col] <= 122)))
                        {
                            if ((int)board[row, col] >= 65 && (int)board[row, col] <= 90)
                            {
                                blackScore += (int)board[row, col];
                            }
                            else
                            {
                                whiteScore += (int)board[row, col];
                            }
                        }
                    }
                    oddOrEven++;
                }
            }
            if (whiteScore > blackScore)
            {
                Console.WriteLine("The winner is: white team");
                Console.WriteLine(whiteScore - blackScore);
            }
            else if (blackScore > whiteScore)
            {
                Console.WriteLine("The winner is: black team");
                Console.WriteLine(blackScore - whiteScore);
            }
            else
            {
                Console.WriteLine("Equal result: {0}", whiteScore);
            }
        }
    }
}

我得到了错误的答案。

我进入

5软件大学_2345

我期待

获胜者是:黑人队41

但我收到

获胜者是:白人队22

我就是想不通。我一直在努力改变球队的位置。此外,我认为我在检查信件是否大写时的表达方式可能有问题。

我在您的循环中做了一些更改:

for (int row = 0; row < boardSize; row++) //calculating score
{
    for (int col = 0; col < boardSize; col++)
    {
        char c = board[row, col];
        if (oddOrEven % 2 == 0)
        {
            if (char.IsLetterOrDigit(c))
            {
                if (char.IsUpper(c))
                {
                    whiteScore += c;
                }
                else
                {
                    blackScore += c;
                }
                Console.WriteLine(c);
            }
        }
        else
        {
            if (char.IsLetterOrDigit(c))
            {
                if (char.IsUpper(c))
                {
                    blackScore += c;
                }
                else
                {
                    whiteScore += c;
                }
                Console.WriteLine(c);
            }
        }
        oddOrEven++;
    }
}

它现在就像一个符咒。问题是您检查字符是否有效的方式。.Net框架充满了有用的工具,请不要犹豫使用它们。我刚刚用char.IsLetterOrDigit(c)救了你。

最新更新