SEO-Software von Suchmaschinenoptimierung.de

4 Gewinnt problem

(hier klicken, um zum Original Thread zu gelangen)
danny_k
Hi ich bin Wirtschaftsinformatiker und habe nun folgenden code soweit fertig


Mein problem ist zur zeit bei diesem 4 gewinnt der befehl void init
Das spiel soll mit disem befehl das gesamte spiel "leer" (mit '_' initieren
kein plan wie ich das mache
Hilfe bitte !!!

code:
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:
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
  }


  /* messages */
  private void promptPlayerMessage(int p) {
    //TODO implement
	  System.out.println("Spielerszug "+p);
  }

  private void rowFullMessage(int x) {
    //TODO implement
	  System.out.println("Die Reihe ist voll");
  }

  private void winMessage(int p) {
    //TODO implement
	  System.out.println("Es gewinnt Spieler" +p);
  }

  private void drawMessage() {
    //TODO implement
	  System.out.println("unentschieden keiner gewinnt ihr Affen");
  }

  private void errorMessage() {
    //TODO implement
	  System.out.println("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();
  }
}
marIus
Zitat:
Original von danny_k
Hi ich bin Wirtschaftsinformatiker und habe nun folgenden code soweit fertig


Mein problem ist zur zeit bei diesem 4 gewinnt der befehl void init
Das spiel soll mit disem befehl das gesamte spiel "leer" (mit '_' initieren
kein plan wie ich das mache
Hilfe bitte !!!


Hi,

was meinst du genau mit leer initieren?
Einfach nur das Spielfeld leer darstellen oder alle Variablen leer machen?

Eigentlich brauchst du doch nur das Spielfeld leer hinterlegen und dann zeichnen. Oder versteh ich dich falsch?
Donut
folgendermaßen:

Die init-funktion erwartet zum einen Einen Zeiger auf char, um anderen die dimension des Feldes. Dies ist leider nötig, da ein zweidimensionales Array eigentlich auchh nur ein eindimensionales ist.

code:
1:
2:
3:
4:
5:
6:
7:
void init(char* feld, int num)
{
	for(int i=0;i<num;i++)
	{
		feld[i]=' ';
	}
}


Allerdings kann man den Aufruf so dynamisch gestalten, dass man denselben effekt als würd man direkt den 2-dinemsionalen Array übergeben:
code:
1:
2:
3:
char spielfeld[10][10];

init(spielfeld[0],sizeof(spielfeld)/sizeof(**spielfeld));
(hier klicken, um zum Original Thread zu gelangen)



Tipp: Ranking-Konzept.de - Das SEO-Forum (Forum rund um die Suchmaschinenoptimierung) der artaxo AG.
Das große Versicherungs ABC von Versicherung.de - mit allen Aspekten der Computerversicherungen!
Fan-Foren.de, die große Community mit Musikforum ist ab sofort online.