Thread komischer fehler aber wo?
(7 answers)
Opened by conray at 2010-04-22 21:31
Hi @ all
wollte hier mal wieder was anhängen aber irgendwie klappt mein code nicht so ganz :D es ist das bekannt spiel XXO. Durchlaufen tut es auch ohne fehler aber die spieler sind immer zwei mal dran ab der 2. runde oO. und die abfrage ob das feld belegt ist funktioniert auch nur halbseitens.. könnt ihr mir da helfen? Sorry for smell code i am a newbie :) Code (perl): (dl
)
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 #/usr/bin/perl use strict; print "What's your name Player1?: "; my $player1 = <STDIN>; chomp $player1; print "\nWhat's your name Player2?: "; my $player2 = <STDIN>; chomp $player2; if ($player1 eq $player2) { $player2 = $player2."2"; } my @field = ("_", "_", "_", "_", "_", "_", "_", "_", "_", "_"); my %valids = (1, 2, 3, 4, 5, 6, 7, 8, 9); my $name = $player1; my $roundcount = 1; my $choose; my $stone = 'x'; my $win = 0; my $othername = $player2; my $tmpname; my $stone2 = 'o'; my $tmpstone; my $valid; system("clear"); while ($roundcount <= 9 ) { print "|$field[7]|$field[8]|$field[9]|\n"; print "|$field[4]|$field[5]|$field[6]|\n"; print "|$field[1]|$field[2]|$field[3]|\n"; print "\n$name your turn\n"; print "In which field do you want to set? "; $choose = <STDIN>; chomp $choose; if (exists $valids{$choose} ) { $valid = 1; } else { $valid = 0; } if ($field[$choose] ne '_') { if ($field[$choose] eq $stone) { print "you had set here before\n"; } else { print "$othername had set here before\n"; } $valid = 0; } else { $field[$choose] = $stone; } if ($field[1] eq $stone and $field[2] eq $stone and $field[3] eq $stone) { $win = 1; } elsif ($field[4] eq $stone and $field[5] eq $stone and $field[6] eq $stone) { $win = 1; } elsif ($field[7] eq $stone and $field[8] eq $stone and $field[9] eq $stone) { $win = 1; } elsif ($field[1] eq $stone and $field[4] eq $stone and $field[7] eq $stone) { $win = 1; } elsif ($field[2] eq $stone and $field[5] eq $stone and $field[8] eq $stone) { $win = 1; } elsif ($field[3] eq $stone and $field[6] eq $stone and $field[9] eq $stone) { $win = 1; } elsif ($field[1] eq $stone and $field[5] eq $stone and $field[9] eq $stone) { $win = 1; } elsif ($field[3] eq $stone and $field[5] eq $stone and $field[7] eq $stone) { $win = 1; } if ($win eq 1) { last; } $roundcount++; print "$win\n$stone\n$name\n$roundcount\n"; if ( $valid eq 1) { $tmpname = $name; $name = $othername; $othername = $tmpname; $tmpstone = $stone; $stone = $stone2; $stone2 = $tmpstone; system("clear"); } else { $roundcount--; } } print "|$field[7]|$field[8]|$field[9]|\n"; print "|$field[4]|$field[5]|$field[6]|\n"; print "|$field[1]|$field[2]|$field[3]|\n"; if ($win eq 1) { print "Congratulations $name you win"; } else { print "--->DRAW<---"; } mfg conray |