2010年2月20日 星期六

Unix Shell Script Tictactoe


#!/bin/csh
set player = -1
set round = 1
set map = (0 0 0 0 0 0 0 0 0)
set result = 0
set loop = 1
set tmp = 0;
while ($loop == 1)
if($player == -1) then
echo "Round $round, team cross turn"
else
echo "Round $round, team circle turn"
endif
echo -n "x = "
set x = $<
echo -n "y = "
set y = $<
@ tmp = ($y - 1) * 3 + $x

if($map[$tmp] != 0) then
echo "Sorry, ($x,$y) has been occupied, team cross please retry."
else
echo -n "game result = "
set result = $<

if($result == W) then
@ map[$tmp] = $player
endif

if($map[1] != 0 && $map[2] != 0 && $map[3] != 0 && $map[4] != 0 && $map[5] != 0 && $map[6] != 0 && $map[7] != 0 && $map[8] != 0 && $map[9] != 0) then
echo "Draw Game"
@ loop = 0
else if(($map[1] == 1 && $map[2] == 1 && $map[3] == 1) || ($map[4] == 1 && $map[5] == 1 && $map[6] == 1) || ($map[7] == 1 && $map[8] == 1 && $map[9] == 1) || ($map[1] == 1 && $map[4] == 1 && $map[7] == 1) || ($map[2] == 1 && $map[8] == 1 && $map[3] == 1) || ($map[3] == 1 && $map[6] == 1 && $map[9] == 1) || ($map[1] == 1 && $map[4] == 1 && $map[9] == 1) || ($map[3] == 1 && $map[4] == 1 && $map[7] == 1)) then
echo "Congratulation!!!"
echo "Team circle wins in round $round"
@ loop = 0
else if(($map[1] == -1 && $map[2] == -1 && $map[3] == -1) || ($map[4] == -1 && $map[5] == -1 && $map[6] == -1) || ($map[7] == -1 && $map[8] == -1 && $map[9] == -1) || ($map[1] == -1 && $map[4] == -1 && $map[7] == -1) || ($map[2] == -1 && $map[8] == -1 && $map[3] == -1) || ($map[3] == -1 && $map[6] == -1 && $map[9] == -1) || ($map[1] == -1 && $map[4] == -1 && $map[9] == -1) || ($map[3] == -1 && $map[4] == -1 && $map[7] == -1)) then
echo "Congratulation!!!"
echo "Team cross wins in round $round"
@ loop = 0
else
@ player *= -1
if($player == -1) then
@ round += 1
endif
endif
endif
end

沒有留言:

張貼留言