游戏无法检测到获胜



我想做一个小游戏。我试着画一条对角线,但没有成功。如果我尝试正常的获胜线,它是有效的,但只在前3个领域。我创建了一个check_winner()函数,用于检测所有获胜字段。我不知道,为什么它不检测获胜场。

这是我的代码

#!/usr/bin/bash
player_1="X"
player_2="O"
turn=1
game_on=true
moves=( - - - - - - - - - )
welcome_message() {
clear
echo "========================"
echo "=== LETS PLAY A GAME ==="
echo "========================"
sleep 3
}
print_board () {
clear
echo " ${moves[0]} | ${moves[1]} | ${moves[2]} "
echo "-----------"
echo " ${moves[3]} | ${moves[4]} | ${moves[5]} "
echo "-----------"
echo " ${moves[6]} | ${moves[7]} | ${moves[8]} "
echo "============="
}
player_pick(){
if [ $(($turn % 2)) == 0 ]
then
echo "Player 1 pick a square: "
play=$player_2
else
play=$player_1
echo "Player 2 pick a sqaure: "
fi
read square
space=${moves[(square -1)]} 



if [[ ${moves[(square-1)]} == "-" ]] && [[ $square = [0-9] ]] || [[ $space = [0-9]  ]]
then 
moves[(square -1)]=$play
((turn+=1))
else

player_pick
echo "Not a valid square."
fi
space=${moves[(square-1)]} 
}
check_match() {
if  [[ ${moves[0]} == ${moves[1]} ]]&& 
[[ ${moves[1]} == ${moves[2]} ]]; then
game_on=false
fi
if [ $game_on == false ]; then
if [ ${moves[$1]} == 'x' ];then
echo "Player one wins!"
return 
else
echo "player two wins!"
return 
fi
fi
}
check_winner(){
if [ $game_on == false ]; then return; fi
check_match 0 1 2
if [ $game_on == false ]; then return; fi
check_match 3 4 5
if [ $game_on == false ]; then return; fi
check_match 6 7 8
if [ $game_on == false ]; then return; fi
check_match 0 4 8
if [ $game_on == false ]; then return; fi
check_match 2 4 6
if [ $game_on == false ]; then return; fi
check_match 0 3 6
if [ $game_on == false ]; then return; fi
check_match 1 4 7
if [ $game_on == false ]; then return; fi
check_match 2 5 8
if [ $game_on == false ]; then return; fi

if [ $turn -gt 9 ]; then 
$game_on=false
echo "Its a draw!"
fi
}

welcome_message
print_board
while $game_on
do
player_pick
print_board
check_winner
done

修改后的版本:

#!/bin/bash
player_1="X"
player_2="O"
turn=1
moves=( - - - - - - - - - )
welcome_message() {
clear
echo "========================"
echo "=== LETS PLAY A GAME ==="
echo "========================"
sleep 3
}
print_board () {
clear
echo " ${moves[0]} | ${moves[1]} | ${moves[2]} "
echo "-----------"
echo " ${moves[3]} | ${moves[4]} | ${moves[5]} "
echo "-----------"
echo " ${moves[6]} | ${moves[7]} | ${moves[8]} "
echo "============="
}
player_pick(){
if [[ $(( turn % 2 )) == 0 ]]
then
echo "Player 1 pick a square: "
play=$player_2
else
play=$player_1
echo "Player 2 pick a square: "
fi
read -r square
if [[ ${moves[(square-1)]} == "-" ]] &&
[[ $square == [1-9] ]]
then
moves[(square -1)]=$play
(( turn += 1 ))
else
echo "Not a valid square."
player_pick
fi
}
check_match() {
index1=$1
index2=$2
index3=$3
if  [[ ${moves[$index1]} == "${moves[$index2]}" ]] &&
[[ ${moves[$index1]} == "${moves[$index3]}" ]] &&
[[ ${moves[$index1]} != "-" ]]
then
if [[ ${moves[$index1]} == 'X' ]]
then
echo "Player one wins!"
exit
else
echo "player two wins!"
exit
fi
fi
}
check_winner(){
if [[ $turn -gt 9 ]]
then
echo "Its a draw!"
exit
fi
check_match 0 1 2
check_match 3 4 5
check_match 6 7 8
check_match 0 4 8
check_match 2 4 6
check_match 0 3 6
check_match 1 4 7
check_match 2 5 8
}
welcome_message
print_board
while true
do
player_pick
print_board
check_winner
done

修改:

  • 完全删除了game_on变量,while现在在true上。
  • player_pick(),我删除了space变量,不需要。
  • check_match()中,创建了3个新变量(index1, index2, index3)来包含参数中收到的值。只是为了让它更清楚,但它们只是$1,$2,$3
  • check_match()中,用新变量替换硬编码索引。一直都有[0],[1][2]。您一直只验证了第一行。
  • check_match(),if [[ ${moves[$index1]} == 'X' ]] ...,你有== 'x'。bash中大写字母很重要。
  • 中的check_winner(),删除所有if语句。check_match()现在退出时,玩家获胜。更小的代码,更容易阅读。
  • inplayer_pick(), "不是一个有效的正方形。"现在在递归调用player_pick()之前打印。

最新更新