algorithm - Chessboard Game Excercise c# -


problem 2 – chessboard game

goshko keen chess player. 1 day bored work , decided take break , create game using chessboard. takes string, e.g. "software university_2345", converts symbols numbers through ascii codes , fills chessboard them. takes values of capital , small letters , digits only. value of other symbol zero. fills board’s squares numbers, left right , top bottom (see example below). size of chessboard n*n (e.g. n = 5) , starts black square. n odd number.

s   o   f   t   w           r   e       u         n     v   e   r         s     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 

let’s assume there 2 competing teams: black team , white team. every team’s score sum of values in squares. if square contains capital letter value should given opposing team. in example above scores calculated 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 input data should read console.

• first line holds size n of chessboard.

• second line holds input string.

the input data valid , in format described. there no need check explicitly.

output output should printed on console.

• first output line holds winning team in format: “the winner is: {name} team”.

• second line holds difference between scores of winning , losing team.

• in case score equal, print “equal result: {points}”. not print difference in case!

here current code:

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 doesnt go out of range of string             int = 0;                                                          //to know element of string store matrix             int whitescore = 0;             int blackscore = 0;             int oddoreven = 0;                                                  //to know if cell odd or              (int row = 0; row < boardsize; row++)                           //filling matrix             {                 if (p == 0)                     break;                  (int col = 0; col < boardsize; col++)                 {                     if (p == 0)                         break;                      board[row, col] = inputstring[i];                     i++;                     p--;                 }             }                                                                               (int row = 0; row < boardsize; row++)                           //calculating score             {                 (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);             }          }     } } 

i getting wrong answers.

i enter

5 software university_2345

i expect

the winner is: black team 41

but receive

the winner is: white team 22

i can't figure out. i've been trying switch teams places. think there might wrong expression checking weather letter capital or not.

i've made few changes in loops:

for (int row = 0; row < boardsize; row++) //calculating score {     (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++;     } } 

it works charm. issue way checking char valid or not. .net framework full of useful tools, don't hesitate use them. here used char.isletterordigit(c) save you.


Comments