Advertisement
Guest User

APPLICATION

a guest
Nov 14th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Eiffel 5.13 KB | None | 0 0
  1. note
  2.     description: "CHESS_BOARD application root class"
  3.     date: "$Date$"
  4.     revision: "$Revision$"
  5.  
  6. class
  7.     APPLICATION
  8.  
  9. inherit
  10.     ARGUMENTS
  11.  
  12. create
  13.     make
  14.  
  15.  
  16. feature {NONE}
  17.  
  18. arr:ARRAY2[PIECE]
  19. temp: PIECE
  20.  
  21.     print_arr
  22.     local
  23.         i,j: INTEGER
  24.     do
  25.  
  26.  
  27.         print("-------------------------%N")
  28.  
  29.         from i:=1
  30.         until i>8
  31.         loop
  32.             print(i) print("| ")
  33.             from j:=1
  34.             until j>8
  35.             loop
  36.                 if (arr.item (j, i) /= void) then
  37.                     print(arr.item (j, i).type.substring (1, 1))
  38.                     if (arr.item (j, i).color) then
  39.                         print("/ ")
  40.                     else
  41.                         print("\ ")
  42.                     end
  43.                 else
  44.                     print("x  ")
  45.                 end
  46.  
  47.                 j:=j+1
  48.             end
  49.             print("%N |%N")
  50.             i:=i+1
  51.         end
  52.  
  53.         print ("   ")
  54.  
  55.         from j:=1
  56.         until j>8
  57.         loop
  58.             print (j) print("  ")
  59.             j:=j+1
  60.         end
  61.  
  62.         print("%N-------------------------%N")
  63.  
  64.     end
  65.  
  66.     make
  67.     local
  68.         i: INTEGER
  69.         ix, iy, ix1, iy1: INTEGER
  70.         do
  71.             create arr.make_filled (void, 8, 8)
  72.  
  73.  
  74. --      Initialize the board
  75. --======================================================================
  76.  
  77.             -- PAWN
  78.             from i:=1
  79.             until i>8
  80.             loop
  81.                 create  temp.make ("pawn", true, i, 2)
  82.                 arr.put (temp, i, 2)
  83.                 create  temp.make ("pawn", false, i, 7)
  84.                 arr.put (temp, i, 7)
  85.                 i:=i+1
  86.             end
  87.  
  88.             create  temp.make ("pawn", true, 4, 4)
  89.             arr.put (temp, 4, 4)
  90.  
  91.  
  92.             -- BISHOP
  93.             create  temp.make ("bishop", true, 3, 1)
  94.             arr.put (temp, 3, 1)
  95.             create  temp.make ("bishop", true, 6, 1)
  96.             arr.put (temp, 6, 1)
  97.             create  temp.make ("bishop", false, 3, 8)
  98.             arr.put (temp, 3, 8)
  99.             create  temp.make ("bishop", false, 6, 8)
  100.             arr.put (temp, 6, 8)
  101.  
  102.             -- KNIGHTs
  103.             create  temp.make ("knight", true, 2, 1)
  104.             arr.put (temp, 2, 1)
  105.             create  temp.make ("knight", true, 7, 1)
  106.             arr.put (temp, 7, 1)
  107.             create  temp.make ("knight", false, 2, 8)
  108.             arr.put (temp, 2, 8)
  109.             create  temp.make ("knight", false, 7, 8)
  110.             arr.put (temp, 7, 8)
  111.  
  112.             -- ROOK
  113.             create  temp.make ("rook", true, 1, 1)
  114.             arr.put (temp, 1, 1)
  115.             create  temp.make ("rook", true, 8, 1)
  116.             arr.put (temp, 8, 1)
  117.             create  temp.make ("queen", false, 1, 7)
  118.             arr.put (temp, 1, 7)
  119.             create  temp.make ("rook", false, 8, 8)
  120.             arr.put (temp, 8, 8)
  121.  
  122.             -- QUEEN
  123.             create  temp.make ("queen", true, 5, 1)
  124.             arr.put (temp, 5, 1)
  125.             create  temp.make ("queen", false, 5, 8)
  126.             arr.put (temp, 5, 8)
  127.  
  128.             -- KING
  129.             create  temp.make ("king", true, 4, 1)
  130.             arr.put (temp, 4, 1)
  131.             create  temp.make ("king", false, 4, 8)
  132.             arr.put (temp, 4, 8)
  133.  
  134. --======================================================================
  135.  
  136.             print("%N%N%N%N%N%N%N%N")
  137.             print_arr
  138.             from i:=0
  139.             until i>2
  140.             loop
  141.                 print("%N Print 0 to terminate %N Input position_from: %N")
  142.                 io.read_integer_32
  143.                 ix:=io.last_integer
  144.                 if (ix=0) then
  145.                     i:=3
  146.                     print("Terminated...")
  147.                 else
  148.                     io.read_integer
  149.                     iy:=io.last_integer
  150.  
  151.  
  152.                     print("%NInput position_to: %N")
  153.                     io.read_integer
  154.                     ix1:=io.last_integer
  155.                     io.read_integer
  156.                     iy1:=io.last_integer
  157.  
  158.                     if (arr.item (ix, iy) /= void and (ix>0 and ix<9 and iy>0 and iy<9 and ix1>0 and ix1<9 and iy1>0 and iy1<9)) then
  159.                         move_to(ix,iy,ix1,iy1)
  160.                         print_arr
  161.                         print("======")
  162.                     else
  163.                         print ("ILLEGAL POSITION!%N")
  164.                     end
  165.  
  166.                 end
  167.  
  168.  
  169.             end
  170.  
  171.         end
  172.  
  173.         move_to(x,y, x1, y1: INTEGER)
  174.         local
  175.             checked_x, checked_y:INTEGER
  176.         do
  177.             checked_x:= x1  checked_y:= y1
  178.             if (arr.item (x, y) /= void) then
  179.                 if (arr.item (x1, y1) /= void) then
  180.                     if (arr.item (x, y).color = arr.item (x1, y1).color) then
  181.                         print("Can't move here"+"%N")
  182.                     elseif (arr.item (x, y).can_move (x1, y1)) then
  183.                         arr.item (x, y).set_newpos (x1, y1)
  184.                         arr.put (arr.item (x, y), x1, y1)
  185.                         arr.put (void, x, y)
  186.  
  187.                         print("ENEMY DOWN" + "%N")
  188.                     end
  189.                 elseif (arr.item (x, y).can_move (x1, y1)) then
  190.                     if (arr.item (x, y).type ~ "queen" or arr.item (x, y).type ~ "rook" or arr.item (x, y).type ~ "bishop") then
  191.                         checked_x:=check_path(x,y,x1,y1)[1]
  192.                         checked_y:=check_path(x,y,x1,y1)[2]
  193.                     end
  194.  
  195.                     if (checked_x = 0 and checked_y = 0) then
  196.                         print("CAN'T MOVE HERE%N")
  197.                     else
  198.                         arr.item (x, y).set_newpos (checked_x, checked_y)
  199.                         arr.put (arr.item (x, y), checked_x, checked_y)
  200.                         arr.put (void, x, y)
  201.  
  202.                         print("SUCCESS" + "%N")
  203.                     end
  204.                 end
  205.  
  206.             end
  207.  
  208.         end
  209.  
  210.  
  211.         check_path(x, y, x1, y1: INTEGER): ARRAY[INTEGER]
  212.         local
  213.             steps, i: INTEGER
  214.             res: ARRAY[INTEGER]
  215.         do
  216.  
  217.             create res.make_filled (0, 1, 2)
  218.             res[1]:=x   res[2]:=y
  219.  
  220.             if ((x-x1).abs >= (y-y1).abs) then
  221.                 steps:=(x-x1).abs
  222.             else
  223.                 steps:=(y-y1).abs
  224.             end
  225.             from i:=1
  226.             until i>steps
  227.             loop
  228.  
  229.                 if (arr.item ((res[1]+(x1-x)//steps), (res[2]+(y1-y)//steps)) /= void) then
  230.                     if (arr.item ((res[1]+(x1-x)//steps), (res[2]+(y1-y)//steps)).color = arr.item (x, y).color) then
  231.                         res[1]:=0       res[2]:=0
  232.                         i:=steps+1
  233.                         RESULT:= res
  234.                     else
  235.                         res[1]:=    (res[1]+(x1-x)//steps)  res[2]:=(res[2]+(y1-y)//steps)
  236.                         i:=steps+1
  237.                         RESULT:= res
  238.                     end
  239.                 else
  240.                     res[1]:=    (res[1]+(x1-x)//steps)  res[2]:=(res[2]+(y1-y)//steps)
  241.                     RESULT:= res
  242.                 end
  243.  
  244.  
  245.                 i:=i+1
  246.             end
  247.  
  248.         end
  249.  
  250. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement