Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- use Tk;
- use strict;
- use warnings;
- $\ = $/;
- my $mw = MainWindow->new;
- $mw->title('Reversi');
- $mw->geometry("512x412");
- my $canvas = $mw->Canvas(
- -width => 400,
- -height => 400,
- -background => "black",
- -relief => "groove"
- )->pack( -side => "left" );
- my @pole;
- my @right = ();
- my $u = 1;
- for ( my $i = 0 ; $i < 8 ; $i++ ) {
- for ( my $j = 0 ; $j < 8 ; $j++ ) {
- $pole[$i][$j] = 0;
- }
- }
- my $x = 10;
- my $y = 10;
- for ( $x = 10 ; ( $x + 40 ) < 400 ; $x += 45 ) {
- for ( $y = 10 ; ( $y + 40 ) < 400 ; $y += 45 ) {
- my $rec = $canvas->createRectangle(
- $x, $y, $x + 40, $y + 40,
- -fill => "red",
- -outline => "orange",
- -width => 2.0
- );
- my $oval = $canvas->createOval(
- $x + 2, $y + 2, $x + 38, $y + 38,
- -fill => "red",
- -outline => "red"
- );
- $canvas->bind( $oval, '<ButtonPress>' => [ \&callBack, $oval ] );
- }
- $y = 10;
- }
- my $exit = $mw->Button(
- -text => 'Exit',
- -command => [ $mw => 'destroy' ]
- )->pack( -side => "right", -padx => 30 );
- $canvas->pack;
- $exit->pack;
- setUp();
- MainLoop;
- sub setUp {
- $pole[3][3] = 1;
- $pole[4][4] = 1;
- $pole[3][4] = 2;
- $pole[4][3] = 2;
- $u = 2;
- push @right, 3, 4, 4, 3;
- draw();
- $u = 1;
- push @right, 3, 3, 4, 4;
- draw();
- }
- sub callBack {
- my $c;
- my $d;
- $b = $_[1];
- $c = int( ( $b - 2 ) / 16 );
- $d = ( ( $b - 2 ) % 16 ) / 2;
- hod( $c, $d );
- }
- sub f {
- my $x = shift; #координаты
- my $y = shift;
- my $sdvig_x = shift; # и на сколько мы их сдвигаем
- my $sdvig_y = shift;
- my @ther = ();
- $x += $sdvig_x;
- $y += $sdvig_y;
- while ( $x < 8 && $x >= 0 && $y < 8 && $y >= 0 ) { #цикл начался
- if ( $pole[$x][$y] == 0 ) { #если 0 то выходим
- last;
- }
- if ( $pole[$x][$y] ==
- ( $u == 1 ? 2 : 1 ) ) #если 1 кладём 2 иначе 1
- { # если вражеские то кладём
- push @ther, $x, $y;
- print $x, $y, $pole[$x][$y];
- $x += $sdvig_x;
- $y += $sdvig_y;
- }
- if ( $pole[$x][$y] == $u ) {
- last;
- }
- }
- if ( ( scalar @ther ) > 1 ) {
- push @right, @ther;
- }
- #конец цикла
- }
- #каждый элимент поля 1 - чёрный,2 - белый,0 - пустой
- #функция которая говорит сделан ли ход
- sub hod {
- #берем координаты хода
- $a = $_[0];
- $b = $_[1];
- #проверка в поисках фишки того же юзера
- #проверка по строкам
- f( $a, $b, +1, 0 );
- f( $a, $b, -1, 0 );
- #проверка по столбцам
- f( $a, $b, 0, -1 );
- f( $a, $b, 0, +1 );
- #диагональки
- f( $a, $b, -1, -1 );
- f( $a, $b, +1, +1 );
- f( $a, $b, +1, -1 );
- f( $a, $b, -1, +1 );
- for ( my $r = 0 ; $r < $#pole ; $r++ ) {
- for ( my $t = 0 ; $t < $#pole ; $t++ ) {
- print $pole[$t][$r];
- }
- }
- if ( scalar @right == 0 ) { print "Can't do that! \n"; }
- else {
- push @right, $a, $b;
- #print $a, $b;
- $u = $u == 1 ? 2 : 1; #переход хода
- for ( my $i = 0 ; $i < ( scalar @right ) ; $i += 2 ) {
- $pole[ $right[$i] ][ $right[ $i + 1 ] ] = $u;
- } #меняем состояние нужной клетки
- draw();
- }
- }
- sub draw {
- for ( my $i = 0 ; $i < ( scalar @right ) ; $i += 2 ) {
- my $id = $right[$i] * 16 + $right[ $i + 1 ] * 2 + 2;
- if ( $u == 2 ) {
- $canvas->itemconfigure( $id, -fill => "black" );
- }
- else {
- $canvas->itemconfigure( $id, -fill => "white" );
- }
- }
- @right = ();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement