Registrierung Kalender Mitgliederliste Teammitglieder Suche Häufig gestellte Fragen Zur Startseite

Informatiker Board » Themengebiete » Informatik in der Schule » spiel tic tac toe programmieren (processing) » Antwort erstellen » Hallo Gast [Anmelden|Registrieren]

Antwort erstellen
Benutzername: (du bist nicht eingeloggt!)
Thema:
Nachricht:

HTML ist nicht erlaubt
BBCode ist erlaubt
Smilies sind erlaubt
Bilder sind erlaubt

Smilies: 21 von 33
smileWinkDaumen hoch
verwirrtAugenzwinkerngeschockt
Mit ZungeGottunglücklich
Forum Kloppebösegroßes Grinsen
TanzentraurigProst
TeufelSpamWillkommen
LehrerLOL HammerZunge raus
Hilfe 
aktuellen Tag schließen
alle Tags schließen
fettgedruckter Textkursiver Textunterstrichener Text zentrierter Text Hyperlink einfügenE-Mail-Adresse einfügenBild einfügen Zitat einfügenListe erstellen CODE einfügenPHP CODE farbig hervorheben
Spamschutz:
Text aus Bild eingeben
Spamschutz

Die letzten 2 Beiträge
Karlito

Hallo anskak,

du prüfst immer nur eine Grenze bei den Feldern und nicht beide je Koordinate. Schau dir mal die Zeilen 25 und 26 an.

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:
int [][]spielfeld=new int [4][4];
float a=random(1);
int spieler= (int)(a+0.5);

void setup() {
  size(700, 500);
  background(255);
  PImage myImage = loadImage ("bunt.jpg");
  image (myImage, 0, 0);
}

void draw() {
  textSize(60);
  fill(0);
  text("TIC TAC TOE", 150, 80);
  stroke(0);
  strokeWeight(5);
  line(200, 250, 500, 250);
  line(200, 350, 500, 350);
  line(300, 150, 300, 450);
  line(400, 150, 400, 450);
  if (mousePressed) {
    for (int x=1;x<4;x++) {
      for (int y=1;y<4;y++) {
        if (mouseX<200+(x*100) && mouseY<150+(y*100) //obere grenze 
            && mouseX>200+((x-1)*100) && mouseY>150+((y-1)*100)){ //untere grenze
          if (spieler==1 && mouseButton==LEFT&& spielfeld[x][y]!=2)
          {
            line(125+(100*x), 75+(100*y), 175+(100*x), 125+(100*y));
            line(175+(100*x), 75+(100*y), 125+(100*x), 125+(100*y));
            spieler=0;
            spielfeld[x][y]=1;
          }
          if (spieler==0 && mouseButton==RIGHT && spielfeld[x][y]!=1)
          {
            noFill();
            ellipse(150+(x*100), 100+(y*100), 50, 50);
            spieler=1;
            spielfeld[x][y]=2;
          }
        }
      }
    }
  }
} 


Wenn der Code so unübersichtlich wird, empfielt es sich übrigens, Teile auszulagern:

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:
int [][]spielfeld=new int [4][4];
float a=random(1);
int spieler= (int)(a+0.5);

void setup() {
  size(700, 500);
  background(255);
  PImage myImage = loadImage ("bunt.jpg");
  image (myImage, 0, 0);*/
}

void checkAndSet(int x, int y){
  if (mouseX<200+(x*100) && mouseY<150+(y*100) //obere grenze 
      && mouseX>200+((x-1)*100) && mouseY>150+((y-1)*100)){ //untere grenze
    if (spieler==1 && mouseButton==LEFT&& spielfeld[x][y]!=2)
    {
      line(125+(100*x), 75+(100*y), 175+(100*x), 125+(100*y));
      line(175+(100*x), 75+(100*y), 125+(100*x), 125+(100*y));
      spieler=0;
      spielfeld[x][y]=1;
    }
    if (spieler==0 && mouseButton==RIGHT && spielfeld[x][y]!=1)
    {
      noFill();
      ellipse(150+(x*100), 100+(y*100), 50, 50);
      spieler=1;
      spielfeld[x][y]=2;
    }
  }
}

void draw() {
  textSize(60);
  fill(0);
  text("TIC TAC TOE", 150, 80);
  stroke(0);
  strokeWeight(5);
  line(200, 250, 500, 250);
  line(200, 350, 500, 350);
  line(300, 150, 300, 450);
  line(400, 150, 400, 450);
  if (mousePressed) {
    for (int x=1;x<4;x++) {
      for (int y=1;y<4;y++) {
        checkAndSet(x,y);        
      }
    }
  }
} 


Außerdem möchte ich noch anmerken, dass man keine 2 ineinander geschachtelten for-Schlefen benötig, da man die Feldposition mit Division ohne Rest direkt berechnen kann. Ich habe dann noch weitere Modifiikationen vorgenommen. Dadurch wird das Programm zwar länger, aber ich denke es wird dadurch trotzdem in gewisser Weise, wenn auch nicht auf den ersten Blick, besser verständlich und wartbar.

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:
int [][]spielfeld=new int [4][4];
float a=random(1);
int spieler= (int)(a+0.5);

void setup() {
  int i;
  size(700, 500);
  background(255);
  //initialisiere Feld so dass die Koordinaten 
  //mit 0 immer -1 sind
  for(i=0;i<3;i++){
    spielfeld[0][i] = -1;
    spielfeld[i][0] = -1;
  }
  PImage myImage = loadImage ("bunt.jpg");
  image (myImage, 0, 0);
}

int getFeldY(){
  int y;
  y = (mouseY-150)/100 + 1;
 
 //check ob gültiges Spielfeld
 if(y>3||y<1){
   y=0; //kein gültiges Feld
 }
 
 return y;
}

int getFeldX(){
 int x; 
 x = (mouseX-200)/100 + 1;
 
 //check ob gültiges Spielfeld 
 if(x>3||x<1){
   x=0; //kein gültiges Feld
 }
 
 return x;
}

void drawCross(int x, int y){
  line(125+(100*x), 75+(100*y), 175+(100*x), 125+(100*y));
  line(175+(100*x), 75+(100*y), 125+(100*x), 125+(100*y));
}

void drawCircle(int x, int y){
  noFill();
  ellipse(150+(x*100), 100+(y*100), 50, 50);
}

int getSpielFeldWert(int x, int y){
  return spielfeld[x][y];
}

void setSpielFeldWert(int x, int y, int wert){
  spielfeld[x][y] = wert;
}

void draw() {
  int x,y,wert;
  textSize(60);
  fill(0);
  text("TIC TAC TOE", 150, 80);
  stroke(0);
  strokeWeight(5);
  line(200, 250, 500, 250);
  line(200, 350, 500, 350);
  line(300, 150, 300, 450);
  line(400, 150, 400, 450);
  if (mousePressed) {
    x = getFeldX();
    y = getFeldY();
    wert = getSpielFeldWert(x,y);
    
    if(wert==0){
      if(spieler==1 && mouseButton==LEFT){
        drawCross(x,y);
        setSpielFeldWert(x,y,1);
        spieler = 0;
      }
      if(spieler==0 && mouseButton==RIGHT){
        drawCircle(x,y);
        setSpielFeldWert(x,y,2);
        spieler = 1;
      }
    }
  }
}


VG,

Karlito
anskak spiel tic tac toe programmieren (processing)

Meine Frage:
Ich muss für die Schule etwas mit Processing programmieren und habe mich für Tic Tac Toe entschieden. Leider bin ich noch ein ziemlicher Anfänger und habe bereits ein Problem gefunden, dass ich nicht schaffe alleine zu lösen.
Ich arbeite gerade daran, dass es nicht möglich ist ein Feld doppelt zu besetzen, nur leider schaffe ich es irgendwie nicht, dass y nicht um eins erhöht wird wenn ich zweimal auf ein Feld klicke.

Meine Ideen:
int [][]spielfeld=new int [4][4];

float a=random(1);

int spieler= (int)(a+0.5);

void setup()

{

size(700, 500);

background(255);

PImage myImage = loadImage ("bunt.jpg");

image (myImage, 0, 0);

}

void draw()

{

textSize(60);

fill(0);

text("TIC TAC TOE", 150, 80);

stroke(0);

strokeWeight(5);

line(200, 250, 500, 250);

line(200, 350, 500, 350);

line(300, 150, 300, 450);

line(400, 150, 400, 450);

if (mousePressed) {

for (int x=1;x<4;x++) {

for (int y=1;y<4;y++) {

if (mouseX<200+(x*100) && mouseY<150+(y*100) && spieler==1 && mouseButton==LEFT&& spielfeld[x][y]!=2)

{

line(125+(100*x), 75+(100*y), 175+(100*x), 125+(100*y));

line(175+(100*x), 75+(100*y), 125+(100*x), 125+(100*y));

spieler=0;

spielfeld[x][y]=1;

}

if ( mouseX<200+(x*100) && mouseY<150+(y*100) && spieler==0 && mouseButton==RIGHT && spielfeld[x][y]!=1)

{
noFill();

ellipse(150+(x*100), 100+(y*100), 50, 50);

spieler=1;

spielfeld[x][y]=2;

}

}

}

}

}