Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- note
- description: "CHESS_BOARD application root class"
- date: "$Date$"
- revision: "$Revision$"
- class
- APPLICATION
- inherit
- ARGUMENTS
- create
- make
- feature {NONE}
- arr:ARRAY2[PIECE]
- temp: PIECE
- print_arr
- local
- i,j: INTEGER
- do
- print("-------------------------%N")
- from i:=1
- until i>8
- loop
- print(i) print("| ")
- from j:=1
- until j>8
- loop
- if (arr.item (j, i) /= void) then
- print(arr.item (j, i).type.substring (1, 1))
- if (arr.item (j, i).color) then
- print("/ ")
- else
- print("\ ")
- end
- else
- print("x ")
- end
- j:=j+1
- end
- print("%N |%N")
- i:=i+1
- end
- print (" ")
- from j:=1
- until j>8
- loop
- print (j) print(" ")
- j:=j+1
- end
- print("%N-------------------------%N")
- end
- make
- local
- i: INTEGER
- ix, iy, ix1, iy1: INTEGER
- do
- create arr.make_filled (void, 8, 8)
- -- Initialize the board
- --======================================================================
- -- PAWN
- from i:=1
- until i>8
- loop
- create temp.make ("pawn", true, i, 2)
- arr.put (temp, i, 2)
- create temp.make ("pawn", false, i, 7)
- arr.put (temp, i, 7)
- i:=i+1
- end
- create temp.make ("pawn", true, 4, 4)
- arr.put (temp, 4, 4)
- -- BISHOP
- create temp.make ("bishop", true, 3, 1)
- arr.put (temp, 3, 1)
- create temp.make ("bishop", true, 6, 1)
- arr.put (temp, 6, 1)
- create temp.make ("bishop", false, 3, 8)
- arr.put (temp, 3, 8)
- create temp.make ("bishop", false, 6, 8)
- arr.put (temp, 6, 8)
- -- KNIGHTs
- create temp.make ("knight", true, 2, 1)
- arr.put (temp, 2, 1)
- create temp.make ("knight", true, 7, 1)
- arr.put (temp, 7, 1)
- create temp.make ("knight", false, 2, 8)
- arr.put (temp, 2, 8)
- create temp.make ("knight", false, 7, 8)
- arr.put (temp, 7, 8)
- -- ROOK
- create temp.make ("rook", true, 1, 1)
- arr.put (temp, 1, 1)
- create temp.make ("rook", true, 8, 1)
- arr.put (temp, 8, 1)
- create temp.make ("queen", false, 1, 7)
- arr.put (temp, 1, 7)
- create temp.make ("rook", false, 8, 8)
- arr.put (temp, 8, 8)
- -- QUEEN
- create temp.make ("queen", true, 5, 1)
- arr.put (temp, 5, 1)
- create temp.make ("queen", false, 5, 8)
- arr.put (temp, 5, 8)
- -- KING
- create temp.make ("king", true, 4, 1)
- arr.put (temp, 4, 1)
- create temp.make ("king", false, 4, 8)
- arr.put (temp, 4, 8)
- --======================================================================
- print("%N%N%N%N%N%N%N%N")
- print_arr
- from i:=0
- until i>2
- loop
- print("%N Print 0 to terminate %N Input position_from: %N")
- io.read_integer_32
- ix:=io.last_integer
- if (ix=0) then
- i:=3
- print("Terminated...")
- else
- io.read_integer
- iy:=io.last_integer
- print("%NInput position_to: %N")
- io.read_integer
- ix1:=io.last_integer
- io.read_integer
- iy1:=io.last_integer
- 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
- move_to(ix,iy,ix1,iy1)
- print_arr
- print("======")
- else
- print ("ILLEGAL POSITION!%N")
- end
- end
- end
- end
- move_to(x,y, x1, y1: INTEGER)
- local
- checked_x, checked_y:INTEGER
- do
- checked_x:= x1 checked_y:= y1
- if (arr.item (x, y) /= void) then
- if (arr.item (x1, y1) /= void) then
- if (arr.item (x, y).color = arr.item (x1, y1).color) then
- print("Can't move here"+"%N")
- elseif (arr.item (x, y).can_move (x1, y1)) then
- arr.item (x, y).set_newpos (x1, y1)
- arr.put (arr.item (x, y), x1, y1)
- arr.put (void, x, y)
- print("ENEMY DOWN" + "%N")
- end
- elseif (arr.item (x, y).can_move (x1, y1)) then
- if (arr.item (x, y).type ~ "queen" or arr.item (x, y).type ~ "rook" or arr.item (x, y).type ~ "bishop") then
- checked_x:=check_path(x,y,x1,y1)[1]
- checked_y:=check_path(x,y,x1,y1)[2]
- end
- if (checked_x = 0 and checked_y = 0) then
- print("CAN'T MOVE HERE%N")
- else
- arr.item (x, y).set_newpos (checked_x, checked_y)
- arr.put (arr.item (x, y), checked_x, checked_y)
- arr.put (void, x, y)
- print("SUCCESS" + "%N")
- end
- end
- end
- end
- check_path(x, y, x1, y1: INTEGER): ARRAY[INTEGER]
- local
- steps, i: INTEGER
- res: ARRAY[INTEGER]
- do
- create res.make_filled (0, 1, 2)
- res[1]:=x res[2]:=y
- if ((x-x1).abs >= (y-y1).abs) then
- steps:=(x-x1).abs
- else
- steps:=(y-y1).abs
- end
- from i:=1
- until i>steps
- loop
- if (arr.item ((res[1]+(x1-x)//steps), (res[2]+(y1-y)//steps)) /= void) then
- if (arr.item ((res[1]+(x1-x)//steps), (res[2]+(y1-y)//steps)).color = arr.item (x, y).color) then
- res[1]:=0 res[2]:=0
- i:=steps+1
- RESULT:= res
- else
- res[1]:= (res[1]+(x1-x)//steps) res[2]:=(res[2]+(y1-y)//steps)
- i:=steps+1
- RESULT:= res
- end
- else
- res[1]:= (res[1]+(x1-x)//steps) res[2]:=(res[2]+(y1-y)//steps)
- RESULT:= res
- end
- i:=i+1
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement