-
Notifications
You must be signed in to change notification settings - Fork 0
/
BorrowedCode.cs
152 lines (132 loc) · 6.47 KB
/
BorrowedCode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#nullable enable
using System;
using static System.Console;
namespace Bme121
{
static partial class Program
{
// ----------------------------------------------------------------------------------------
// Return the single-character string "a".."z" corresponding to its index 0..25.
// Return " " for an invalid index.
static string LetterAtIndex( int number )
{
if( number < 0 || number > 25 ) return " ";
else return "abcdefghijklmnopqrstuvwxyz"[ number ].ToString( );
}
// ----------------------------------------------------------------------------------------
// Return the index 0..25 corresponding to its single-character string "a".."z".
// Return -1 for an invalid string.
static int IndexAtLetter( string letter )
{
if( letter.Length != 1 ) return -1;
else return "abcdefghijklmnopqrstuvwxyz".IndexOf( letter[ 0 ] );
}
// ----------------------------------------------------------------------------------------
// Create a new Othello game board, initialized with four pieces in their starting
// positions. The counts of rows and columns must be no less than 4, no greater than 26,
// and not an odd number. If not, the new game board is created as an empty array.
static string[ , ] NewBoard( int rows, int cols )
{
const string blank = " ";
const string white = "O";
const string black = "X";
if( rows < 4 || rows > 26 || rows % 2 == 1
|| cols < 4 || cols > 26 || cols % 2 == 1 ) return new string[ 0, 0 ];
string[ , ] board = new string[ rows, cols ];
for( int row = 0; row < rows; row ++ )
{
for( int col = 0; col < cols; col ++ )
{
board[ row, col ] = blank;
}
}
board[ rows / 2 - 1, cols / 2 - 1 ] = white;
board[ rows / 2 - 1, cols / 2 ] = black;
board[ rows / 2, cols / 2 - 1 ] = black;
board[ rows / 2, cols / 2 ] = white;
return board;
}
// ----------------------------------------------------------------------------------------
// Display the Othello game board on the Console.
// All information about the game is held in the two-dimensional string array.
static void DisplayBoard( string[ , ] board )
{
const string h = "\u2500"; // horizontal line
const string v = "\u2502"; // vertical line
const string tl = "\u250c"; // top left corner
const string tr = "\u2510"; // top right corner
const string bl = "\u2514"; // bottom left corner
const string br = "\u2518"; // bottom right corner
const string vr = "\u251c"; // vertical join from right
const string vl = "\u2524"; // vertical join from left
const string hb = "\u252c"; // horizontal join from below
const string ha = "\u2534"; // horizontal join from above
const string hv = "\u253c"; // horizontal vertical cross
const string mx = "\u256c"; // marked horizontal vertical cross
const string sp = " "; // space
// Nothing to display?
if( board == null ) return;
int rows = board.GetLength( 0 );
int cols = board.GetLength( 1 );
if( rows == 0 || cols == 0 ) return;
// Display the board row by row.
for( int row = 0; row < rows; row ++ )
{
if( row == 0 )
{
// Labels above top edge.
for( int col = 0; col < cols; col ++ )
{
if( col == 0 ) Write( " {0}{0}{1}{0}", sp, LetterAtIndex( col ) );
else Write( "{0}{0}{1}{0}", sp, LetterAtIndex( col ) );
}
WriteLine( );
// Border above top row.
for( int col = 0; col < cols; col ++ )
{
if( col == 0 ) Write( " {0}{1}{1}{1}", tl, h );
else Write( "{0}{1}{1}{1}", hb, h );
if( col == cols - 1 ) Write( "{0}", tr );
}
WriteLine( );
}
else
{
// Border above a row which is not the top row.
for( int col = 0; col < cols; col ++ )
{
if( rows > 5 && cols > 5 && row == 2 && col == 2
|| rows > 5 && cols > 5 && row == 2 && col == cols - 2
|| rows > 5 && cols > 5 && row == rows - 2 && col == 2
|| rows > 5 && cols > 5 && row == rows - 2 && col == cols - 2 )
Write( "{0}{1}{1}{1}", mx, h );
else if( col == 0 ) Write( " {0}{1}{1}{1}", vr, h );
else Write( "{0}{1}{1}{1}", hv, h );
if( col == cols - 1 ) Write( "{0}", vl );
}
WriteLine( );
}
// Row content displayed column by column.
for( int col = 0; col < cols; col ++ )
{
if( col == 0 ) Write( " {0,-2}", LetterAtIndex( row ) ); // Labels on left side
Write( "{0} {1} ", v, board[ row, col ] );
if( col == cols - 1 ) Write( "{0}", v );
}
WriteLine( );
if( row == rows - 1 )
{
// Border below last row.
for( int col = 0; col < cols; col ++ )
{
if( col == 0 ) Write( " {0}{1}{1}{1}", bl, h );
else Write( "{0}{1}{1}{1}", ha, h );
if( col == cols - 1 ) Write( "{0}", br );
}
WriteLine( );
}
}
}
}
}
Credit: George Freeman, BME 121 Professor @ The University of Waterloo (uwaterloo.ca/electrical-computer-engineering/profile/freeman)