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: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270:
| using System; using System.Text.RegularExpressions;
namespace Spiel { public class vierGewinnt { public class game { public string[,] board = new string[10, 18];
public game() { for (int j = 0; j < 9; j++) { for (int i = 0; i < 15; i += 1) { if (i % 2 == 0) board[j, i] = "|"; else board[j, i] = " "; } }
for (int j = 0; j < 10; j++) { for (int i = 0; i < 18; i += 1) { Console.Write(board[j, i]); } Console.Write("\n"); } Console.WriteLine(" 1 2 3 4 5 6 7"); }
public void output() { for (int j = 0; j < 10; j++) { for (int i = 0; i < 18; i += 1) { Console.Write(board[j, i]); } Console.Write("\n"); } Console.WriteLine(" 1 2 3 4 5 6 7"); }
public bool evaluate(string[,] game) { string[] horizontal = new string[80]; int k = 0;
for (int j = 0; j < 10; j++) { for (int i = 1; i < 16; i += 2) { horizontal[k] = game[j, i]; k += 1; } }
string[] vertical = new string[80]; k = 0;
for (int j = 1; j < 16; j += 2) { for (int i = 0; i < 10; i++) { vertical[k] = game[i, j]; k += 1; } }
string[] diagonal = new string[160]; k = 0;
for (int h = 0; h < 9; h++) { int i = h; int j = 1; do { diagonal[k] = game[i, j]; k++; i++; j += 2; } while (i < 9); diagonal[k] = " "; k++; }
for (int h = 3; h <= 16; h += 2) { int j = h; int i = 0; do { diagonal[k] = game[i, j]; k++; i++; j += 2; } while (j <= 16); diagonal[k] = " "; k++; }
for (int h = 0; h < 9; h++) { int i = h; int j = 17; do { diagonal[k] = game[i, j]; k++; i++; j -= 2; } while (i < 9); diagonal[k] = " "; k++; }
for (int h = 15; h <= 16; h += 2) { int j = h; int i = 0; do { diagonal[k] = game[i, j]; k++; i++; j += 2; } while (j <= 16); diagonal[k] = " "; k++; }
int rowHorizontal = 1; int rowVertical = 1; int rowDiagonal = 1; int s = 0; while (s < horizontal.Length) { if ((horizontal[s] == "O" || horizontal[s] == "X") && (horizontal[s] == horizontal[s + 1])) rowHorizontal += 1; else rowHorizontal = 1; if ((vertical[s] == "O" || vertical[s] == "X") && (vertical[s] == vertical[s + 1])) rowVertical += 1; else rowVertical = 1;
if (rowHorizontal == 4 || rowVertical == 4) { Console.WriteLine("GEWONNEN"); return true; } s++; }
s = 0; while (s < diagonal.Length) { if (diagonal[s] == "O" || diagonal[s] == "X") rowDiagonal += 1; else rowDiagonal = 1;
if (rowDiagonal == 4) { Console.WriteLine("GEWONNEN"); return true; } s++; } return false; } }
public class player { string Name; public string Sign;
public player(string Name, string Sign) { this.Name = Name; this.Sign = Sign; }
public int move() { Console.WriteLine("Player {0}: Choose a column!", Name); int col = Int32.Parse(Console.ReadLine()!); return col; } }
static void Main() { player AA = new player("A", "X"); player BB = new player("B", "O"); game A = new game();
int column; bool end = false;
while (!end) { column = AA.move() * 2 - 1;
for (int j = 9; j >= 0; j--) { if (A.board[j, column] == "|") { Console.WriteLine("error"); break; }
else if (A.board[j, column] == " ") { A.board[j, column] = AA.Sign; break; }
else if (A.board[j, column] == "X" || A.board[j, column] == "O") { continue; } } Console.Clear(); A.output(); end = A.evaluate(A.board);
if (!end) { column = BB.move() * 2 - 1;
for (int j = 9; j >= 0; j--) { if (A.board[j, column] == "|") { Console.WriteLine("error"); break; }
if (A.board[j, column] == " ") { A.board[j, column] = BB.Sign; break; }
else if (A.board[j, column] == "X" || A.board[j, column] == "O") { continue; }
} Console.Clear(); A.output(); end = A.evaluate(A.board); } else break; } } } } |