|
|
Java Quellcode |
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 |
public class ConnectFour extends ConsoleProgram /* DialogProgram */{ // state variables of this object int width = 7; int height = 6; char[][] board = new char[width][height]; /** * Main loop of the program. Let players take turns entering moves until one * player wins or the board is full, in which case the game is a draw. */ public void run() { int player = 1; println("---- 4 GEWINNT -----"); // no winner yet. int winner = 0; while (winner == 0 && !isFull()) { // print the current board printBoard(); // prompt the current player to enter a move promptPlayerMessage(player); // read the move from the player int x = readInt(); // terminate when -1 is entered if (x == -1) System.exit(0); // players count from 1, Java from 0 x--; // is input in correct range? if (x >= 0 && x < width) { // valid move, perform it int y = putInColumn(x, player); if (y == -1) // output row is full message rowFullMessage(x); else if (hasWon(x, y)) winner = player; else // next player player = 3 - player; } else // invalid move errorMessage(); } // game has ended, either we have a winner or a draw if (winner == 0) drawMessage(); else winMessage(winner); } /** * Initializes the Connect Four Game * */ public ConnectFour() { init(); } /** * Returns the char that represents a player (i.e. x or o) * * @param playerNo * number of the player, i.e. 1 or 2 * @return the char for the player */ public char getPlayerChar(int playerNo) { char[] playerChars = { 'x', 'o' }; return playerChars[playerNo - 1]; } /** * Prints the current board For example: * | * | * | * | o x * | o x x * |o x x o x * ------------- * 1 2 3 4 5 6 7 * */ public void printBoard() { StringBuffer sb = new StringBuffer(); for (int i = 0; i < height; i++) { // start of a line sb.append("|"); for (int j = 0; j < width; j++) { sb.append(board[j][i]).append(' '); } // end of a line sb.append('\n'); } sb.append(" -------------\n"); sb.append(" 1 2 3 4 5 6 7"); println(sb.toString()); } /** * Checks whether the game is won and who won the game * * @return the number of the winner (i.e. 1 or 2) or 0 if no one has yet won * the game */ public boolean hasWon(int x, int y) { return (hasFourHorizontally(x, y) || hasFourVertically(x, y) || hasFourDiagonally(x, y)); } /** * Checks whether another game piece can be added * * @return true if the board is full */ public boolean isFull() { for (int x = 0; x < width; x++) if (board[x][0] == '_') return false; return true; } /** * Test whether there are four gaming pieces with the given char above each * other. Only checking column x needed as this is the only column a vertical * four-in-a-row could have been created in this turn. * * @param x * column of the last added piece * @param y * row of the last added piece * @return whether their are four gaming pieces above each other */ public boolean hasFourVertically(int x, int y) { //TODO implement return false; } /** * Test whether there are four gaming pieces with the given char next to each * other. Only checking column x needed as this is the only column a horizontal * four-in-a-row could have been created in this turn. * * @param x * column of the last added piece * @param y * row of the last added piece * @return whether their are four gaming pieces next to each other */ public boolean hasFourHorizontally(int x, int y) { //TODO implement return false; } /** * Test whether there are 4 gaming pieces next to each other on a diagonal. * Only checking column x needed as this is the only column a horizontal * four-in-a-row could have been created in this turn. * * @param x * column of the last added piece * @param y * row of the last added piece * @return whether there are 4 gaming pieces next to each other on a diagonal */ public boolean hasFourDiagonally(int x, int y) { //TODO implement return false; } /** * Initialize the board with blanks * */ public void init() { //TODO implement board[6][5] = '_'; } /* messages */ private void promptPlayerMessage(int p) { //TODO implement println("Spielerszug "+p); } private void rowFullMessage(int x) { //TODO implement println("Die Reihe ist voll"); } private void winMessage(int p) { //TODO implement println("Es gewinnt Spieler" +p); } private void drawMessage() { //TODO implement println("unentschieden keiner gewinnt ihr Affen"); } private void errorMessage() { //TODO implement showErrorMessage("Eingabe falsch etwas stimmt nicht!!!"); } /** * Put a gaming piece on top of the gaming pieces in the given column by the */ public int putInColumn(int column, int playerNo) { //TODO put piece of player in column return -1; } /** * Main method. This is automatically called by the environment when your * program starts. * * @param args */ public static void main(String[] args) { new ConnectFour().run(); } } |
This post has been edited 1 times, last edit by "danny_k" (Dec 14th 2008, 10:10pm)
This post has been edited 2 times, last edit by "Hafner" (Dec 14th 2008, 1:09pm)
Sorry Wie funktioniert das mit den tags
leider bin ich noch ziemicher anfänger und habe noch nicht so wirklich plan was ich für die hasFour funktion genau implementieren soll bsp. wäre sehr gut
|
|
Java Quellcode |
1 |
sb.append(board[j]).append(' '); |
|
|
Java Quellcode |
1 |
sb.append(board[j][i]).append(' '); |
|
|
Java Quellcode |
1 |
char[][] board = new char[width][height]; |
This post has been edited 1 times, last edit by "Hafner" (Dec 14th 2008, 9:33pm)
|
|
Java Quellcode |
1 2 3 4 5 6 |
public boolean hasFourHorizontally(int x, int y) { // TODO implement for (x = 0; x < 4; x++) if (board[x][y] == 'x') return true; return false; |
This post has been edited 5 times, last edit by "danny_k" (Dec 15th 2008, 12:35am)
This post has been edited 1 times, last edit by "Hafner" (Dec 15th 2008, 12:24am)
|
|
Java Quellcode |
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 271 272 273 274 275 276 |
import acm.program.ConsoleProgram; import acm.program.DialogProgram; public class ConnectFour extends ConsoleProgram /* DialogProgram */{ // state variables of this object int width = 7; int height = 6; char[][] board = new char[width][height]; /** * Main loop of the program. Let players take turns entering moves until one * player wins or the board is full, in which case the game is a draw. */ public void run() { int player = 1; println("---- 4 GEWINNT -----"); // no winner yet. int winner = 0; while (winner == 0 && !isFull()) { // print the current board printBoard(); // prompt the current player to enter a move promptPlayerMessage(player); // read the move from the player int x = readInt(); // terminate when -1 is entered if (x == -1) System.exit(0); // players count from 1, Java from 0 x--; // is input in correct range? if (x >= 0 && x < width) { // valid move, perform it int y = putInColumn(x, player); if (y == -1) // output row is full message rowFullMessage(x); else if (hasWon(x, y)) winner = player; else // next player player = 3 - player; } else // invalid move errorMessage(); } // game has ended, either we have a winner or a draw if (winner == 0) drawMessage(); else winMessage(winner); } /** * Initializes the Connect Four Game * */ public ConnectFour() { init(); } /** * Returns the char that represents a player (i.e. x or o) * * @param playerNo * number of the player, i.e. 1 or 2 * @return the char for the player */ public char getPlayerChar(int playerNo) { char[] playerChars = { 'x', 'o' }; return playerChars[playerNo - 1]; } /** * Prints the current board For example: | | | |o x |o x x |o x x o x * ------------- 1 2 3 4 5 6 7 * */ public void printBoard() { StringBuffer sb = new StringBuffer(); for (int i = 0; i < height; i++) { // start of a line sb.append("|"); for (int j = 0; j < width; j++) { sb.append(board[j][i]).append(' '); } // end of a line sb.append('\n'); } sb.append(" -------------\n"); sb.append(" 1 2 3 4 5 6 7"); println(sb.toString()); } /** * Checks whether the game is won and who won the game * * @return the number of the winner (i.e. 1 or 2) or 0 if no one has yet won * the game */ public boolean hasWon(int x, int y) { return (hasFourHorizontally(x, y) || hasFourVertically(x, y) || hasFourDiagonally (x, y));} /** * Checks whether another game piece can be added * * @return true if the board is full */ public boolean isFull() { for (int x = 0; x < width; x++) if (board[x][0] == '_') return false; return true; } /** * Test whether there are four gaming pieces with the given char above each * other. Only checking column x needed as this is the only column a * vertical four-in-a-row could have been created in this turn. * * @param x * column of the last added piece * @param y * row of the last added piece * @return whether their are four gaming pieces above each other */ public boolean hasFourVertically(int x, int y) { int win = 0; for (int i = 0; i < height; i++) { if (board[x][i] == board[x][y]) win++; else win = 0; if (win == 4) return true; } return false; } /** * Test whether there are four gaming pieces with the given char next to * each other. Only checking column x needed as this is the only column a * horizontal four-in-a-row could have been created in this turn. * * @param x * column of the last added piece * @param y * row of the last added piece * @return whether their are four gaming pieces next to each other */ public boolean hasFourHorizontally(int x, int y) { // TODO implement // int playersymbol = board [x][y]; int win = 0; for (int i = 0; i < width; i++) { if (board[i][y] == board[x][y]) win++; else win = 0; if (win == 4) return true; } return false; } /** * Test whether there are 4 gaming pieces next to each other on a diagonal. * Only checking column x needed as this is the only column a horizontal * four-in-a-row could have been created in this turn. * * @param i * column of the last added piece * @param j * row of the last added piece * @return whether there are 4 gaming pieces next to each other on a * diagonal */ public boolean hasFourDiagonally(int x, int y) { int playerSymbol = board[x][y]; int i = x; int j = y; int count = 0; while ((i > 0) && (j > 0)) { i--; j--; } while ((i <= 6) && (j <= 5)) { if (board[i][j] == playerSymbol) count++; else count = 0; i++; j++; } if (count == 4) return true; return false; } /** * Initialize the board with blanks * */ public void init() { for (int i = 0; i < width; i++) { // i = höhe for (int j = 0; j < height; j++) { // j = breite board[i][j] = '_'; } } } /* messages */ private void promptPlayerMessage(int p) { // TODO implement println("Spielerszug " + p); } private void rowFullMessage(int x) { // TODO implement println("Die Reihe ist voll"); } private void winMessage(int p) { printBoard(); // TODO implement println("Es gewinnt Spieler " + p); } private void drawMessage() { // TODO implement println("unentschieden keiner gewinnt ihr Affen"); } private void errorMessage() { // TODO implement showErrorMessage("Eingabe falsch etwas stimmt nicht!!!"); } /** * Put a gaming piece on top of the gaming pieces in the given column by the */ public int putInColumn(int column, int playerNo) { // Zählt die höhe herunter bis auf 0 for (int y = height - 1; y >= 0; y--) { // Wenn array '_' das entspricht if (board[column][y] == '_') { board[column][y] = getPlayerChar(playerNo); return y; } } return -1; } /** * Main method. This is automatically called by the environment when your * program starts. * * @param args */ public static void main(String[] args) { new ConnectFour().start(); } } |
|
|
Java Quellcode |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
public boolean hasFourDiagonally(int x, int y) { int playerSymbol = board[x][y]; int Spalte = x; int Hoehe = y; int count = 0; while ((Spalte >= 0) && (Hoehe <= 5)) { Spalte--; Hoehe++; } while ((Spalte < 6) && (Hoehe >= 5)) { if (board[Spalte][Hoehe] == playerSymbol) count++; else count = 0; Spalte++; Hoehe--; } if (count == 4) return true; return false; } |
|
|
Source code |
16 17 18 19 |
count = 0; board[Spalte][Hoehe] = count; Spalte++; Hoehe--; |