TicTacToe Bash脚本

huangapple go评论70阅读模式
英文:

TicTacToe Bash script

问题

我无法选择最后一个方格(3;3)。主要问题就在这里。希望有人能帮助我,因为我无法知道问题出在哪里。

代码在这里:

#!/bin/bash

board=(" " " " " "
        " " " " " "
        " " " " )

function draw_board {
    echo "+---+---+---+"
    echo "| ${board[0]} | ${board[1]} | ${board[2]} |"
    echo "+---+---+---+"
    echo "| ${board[3]} | ${board[4]} | ${board[5]} |"
    echo "+---+---+---+"
    echo "| ${board[6]} | ${board[7]} | ${board[8]}  |"
    echo "+---+---+---+"
}

function check_win {
    local symbol=$1
    if [[ ${board[0]} == $symbol && ${board[1]} == $symbol && ${board[2]} == $symbol ]] ||
       [[ ${board[3]} == $symbol && ${board[4]} == $symbol && ${board[5]} == $symbol ]] ||
       [[ ${board[6]} == $symbol && ${board[7]} == $symbol && ${board[8]} == $symbol ]] ||
       [[ ${board[0]} == $symbol && ${board[3]} == $symbol && ${board[6]} == $symbol ]] ||
       [[ ${board[1]} == $symbol && ${board[4]} == $symbol && ${board[7]} == $symbol ]] ||
       [[ ${board[2]} == $symbol && ${board[5]} == $symbol && ${board[8]} == $symbol ]] ||
       [[ ${board[0]} == $symbol && ${board[4]} == $symbol && ${board[8]} == $symbol ]] ||
       [[ ${board[2]} == $symbol && ${board[4]} == $symbol && ${board[6]} == $symbol ]]; then
        echo true
    else
        echo false
    fi
}

function is_free {
    local index=$1
    if [[ ${board[$index]} == " " ]]; then
        echo true
    else
        echo false
    fi
}

function game_loop {
    local player="X"
    local move_count=0
    local win=false
    while [[ $move_count -lt 10  && $win == false ]]; do
        draw_board
        echo "$player következik. Adja meg az x koordinátát [1-3]: "
        read x
        echo "Adja meg az y koordinátát [1-3]: "
        read y
        x=$((x-1))
        y=$((y-1))
        if [[ $x -lt 0 || $x -gt 2 || $y -lt 0 || $y -gt 2 ]]; then
            echo "Hibás koordináta!" >&2
        elif [[ $(is_free $((y*3+x))) == true ]]; then
            board[$((y*3+x))]= "$player"
            win=$(check_win $player)
            if [[ $win == true ]]; then
                echo "$player győzött!"
            else
                ((move_count++))
                if [[ $player == "X" ]]; then
                    player="O"
                else
                    player="X"
                fi
                echo "Lépéshiba!" >&2
            fi
        fi
    done
    if [[ $win == false ]]; then
        draw_board
        echo "Döntetlen!"
    fi
}

我尝试了3;3,程序没有接受它并让我再试一次。谢谢阅读!如果您有任何解决方案,请与我分享!祝您愉快的一天!

英文:

I can't select the last square (3;3). The main problem was that. I hope someone can help me because i cannot know whats the problem.

The code is here:

#!/bin/bash
board=( " "  " "  " "
" "  " "  " "
" "  " " )
function draw_board {
echo "+---+---+---+"
echo "| ${board[0]} | ${board[1]} | ${board[2]} |"
echo "+---+---+---+"
echo "| ${board[3]} | ${board[4]} | ${board[5]} |"
echo "+---+---+---+"
echo "| ${board[6]} | ${board[7]} | ${board[8]}  |"
echo "+---+---+---+"
}
function check_win {
local symbol=$1
if [[ ${board[0]} == $symbol && ${board[1]} == $symbol && ${board[2]} == $symbol ]] ||
[[ ${board[3]} == $symbol && ${board[4]} == $symbol && ${board[5]} == $symbol ]] ||
[[ ${board[6]} == $symbol && ${board[7]} == $symbol && ${board[8]} == $symbol ]] ||
[[ ${board[0]} == $symbol && ${board[3]} == $symbol && ${board[6]} == $symbol ]] ||
[[ ${board[1]} == $symbol && ${board[4]} == $symbol && ${board[7]} == $symbol ]] ||
[[ ${board[2]} == $symbol && ${board[5]} == $symbol && ${board[8]} == $symbol ]] ||
[[ ${board[0]} == $symbol && ${board[4]} == $symbol && ${board[8]} == $symbol ]] ||
[[ ${board[2]} == $symbol && ${board[4]} == $symbol && ${board[6]} == $symbol ]]; then
echo true
else
echo false
fi
}
function is_free {
local index=$1
if [[ ${board[$index]} == " " ]]; then
echo true
else
echo false
fi
}
function game_loop {
local player="X"
local move_count=0
local win=false
while [[ $move_count -lt 10  && $win == false ]]; do
draw_board
echo "$player következik. Adja meg az x koordinátát [1-3]: "
read x
echo "Adja meg az y koordinátát [1-3]: "
read y
x=$((x-1))
y=$((y-1))
if [[ $x -lt 0 || $x -gt 2 || $y -lt 0 || $y -gt 2 ]]; then
echo "Hibás koordináta!" >&2
elif [[ $(is_free $((y*3+x))) == true ]]; then
board[$((y*3+x))]="$player"
win=$(check_win $player)
if [[ $win == true ]]; then
echo "$player győzött!"
else
((move_count++))
if [[ $player == "X" ]]; then
player="O"
else
player="X"
fi
echo "Lépéshiba!" >&2
fi
fi
done
if [[ $win == false ]]; then
draw_board
echo "Dontetlen!"
fi
}

I tried 3;3 and the program was not accept it and give me another try. Thank you for the reading! If you have any solution please share with me! Have a nice day!

答案1

得分: 1

你原始的脚本是一个不错的终端游戏示例。然而...

在计算网格中的放置位置时存在一些逻辑错误。

此外,我将bash数组向右移动了一个位置,以使3x3矩阵更符合传统数学坐标的表示。

在下面这个脚本的改编中,我还使用了一些通用理解的英语等效字符串来更新消息字符串。

我个人不太喜欢使用elif,因此我将这些代码段展开以提高清晰度(在我的观点中)。希望你喜欢这个更新后的版本。

修改后的脚本:

#!/bin/bash

board=(" "
	" " " " " "
	" " " " " " "
	" " " " " " " )

function draw_board {
    echo -e "\n\t+---+---+---+"
    echo -e "\t| ${board[1]} | ${board[2]} | ${board[3]} |"
    echo -e "\t+---+---+---+"
    echo -e "\t| ${board[4]} | ${board[5]} | ${board[6]} |"
    echo -e "\t+---+---+---+"
    echo -e "\t| ${board[7]} | ${board[8]} | ${board[9]} |"
    echo -e "\t+---+---+---+"
}

function check_win {
    local symbol=${1}
    if [[ ${board[1]} == ${symbol} && ${board[2]} == ${symbol} && ${board[3]} == ${symbol} ]] ||
       [[ ${board[4]} == ${symbol} && ${board[5]} == ${symbol} && ${board[6]} == ${symbol} ]] ||
       [[ ${board[7]} == ${symbol} && ${board[8]} == ${symbol} && ${board[9]} == ${symbol} ]] ||
       [[ ${board[1]} == ${symbol} && ${board[4]} == ${symbol} && ${board[7]} == ${symbol} ]] ||
       [[ ${board[2]} == ${symbol} && ${board[5]} == ${symbol} && ${board[8]} == ${symbol} ]] ||
       [[ ${board[3]} == ${symbol} && ${board[6]} == ${symbol} && ${board[9]} == ${symbol} ]] ||
       [[ ${board[1]} == ${symbol} && ${board[5]} == ${symbol} && ${board[9]} == ${symbol} ]] ||
       [[ ${board[3]} == ${symbol} && ${board[5]} == ${symbol} && ${board[7]} == ${symbol} ]]; then
        echo "true"
    else
        echo "false"
    fi
}


function is_free {
    local index=${1}
    if [[ ${board[${index}]} == " " ]]; then
        echo "true"
    else
        echo "false"
    fi
}

function game_loop {
    local player="X"
    local move_count=0
    local win=false

    draw_board

    while [[ ${move_count} -lt 9 ]]; do
        echo -e "\nPlayer $player:\n\tEnter your choice of x coordinate [1-3]: \c"
        read x
        echo -e "\tEnter your choice of y coordinate [1-3]: \c"
        read y

        if [[ $x -lt 1 || $x -gt 3 || $y -lt 1 || $y -gt 3 ]]; then
            echo -e "\tInvalid coordinate entered!" >&2
        else
            if [[ $(is_free $(( (y-1) * 3 + x )) == true ]]; then
                board[$(( (y-1) * 3 + x ))]="${player}"
                ((move_count++))

                win=$(check_win $player)
                if [[ ${win} == "true" ]]; then
                    draw_board
                    echo -e "\n\t*** ${player} wins Tic-Tac-Toe ***"
                    break
                else
                    if [[ ${player} == "X" ]]; then
                        player="O"
                    else
                        player="X"
                    fi
                fi
            else
                echo -e "\t\t That space is taken.  Please choose another ..."
            fi
        fi
        draw_board
    done

    if [[ ${win} == "false" ]]; then
        echo -e "\n\t*** Draw ***"
    fi
}

game_loop

希望这对你有所帮助。

英文:

Your original script was a nice piece of work for a terminal game. However...

There was some logic error in the calculation of the placement positions in the grid.

Also, I shifted the bash array by one ... in order to make the 3x3 matrix more consistent with traditional mathematical representations of coordinates.

In the below adaptation of the script, I have also updated the message strings with some sensible equivalents in English for common understanding.

I am personally not a fan of elif usage, so I exploded those code segments for more clarity (in my view). I hope you like the updated version.

The modified script:

#!/bin/bash
board=( " "
" "  " "  " "
" "  " "  " "
" "  " "  " " )
function draw_board {
echo -e	"\n\t+---+---+---+"
echo -e	"\t| ${board[1]} | ${board[2]} | ${board[3]} |"
echo -e	"\t+---+---+---+"
echo -e	"\t| ${board[4]} | ${board[5]} | ${board[6]} |"
echo -e	"\t+---+---+---+"
echo -e	"\t| ${board[7]} | ${board[8]} | ${board[9]} |"
echo -e	"\t+---+---+---+"
}
function check_win {
local symbol=${1}
if [[ ${board[1]} == ${symbol} && ${board[2]} == ${symbol} && ${board[3]} == ${symbol} ]] ||
[[ ${board[4]} == ${symbol} && ${board[5]} == ${symbol} && ${board[6]} == ${symbol} ]] ||
[[ ${board[7]} == ${symbol} && ${board[8]} == ${symbol} && ${board[9]} == ${symbol} ]] ||
[[ ${board[1]} == ${symbol} && ${board[4]} == ${symbol} && ${board[7]} == ${symbol} ]] ||
[[ ${board[2]} == ${symbol} && ${board[5]} == ${symbol} && ${board[8]} == ${symbol} ]] ||
[[ ${board[3]} == ${symbol} && ${board[6]} == ${symbol} && ${board[9]} == ${symbol} ]] ||
[[ ${board[1]} == ${symbol} && ${board[5]} == ${symbol} && ${board[9]} == ${symbol} ]] ||
[[ ${board[3]} == ${symbol} && ${board[5]} == ${symbol} && ${board[7]} == ${symbol} ]]; then
echo "true"
else
echo "false"
fi
}
function is_free {
local index=${1}
if [[ ${board[${index}]} == " " ]]; then
echo "true"
else
echo "false"
fi
}
function game_loop {
local player="X"
local move_count=0
local win=false
draw_board
while [[ ${move_count} -lt 9 ]]; do
#echo "$player következik. Adja meg az x koordinátát [1-3]: "
echo -e "\nPlayer $player:\n\tEnter your choice of x coordinate [1-3]: \c"
read x
echo -e "\tEnter your choice of y coordinate [1-3]: \c"
read y
if [[ $x -lt 1 || $x -gt 3 || $y -lt 1 || $y -gt 3 ]]; then
echo -e "\tInvalid coordinate entered!" >&2
else
if [[ $(is_free $(( (y-1) * 3 + x ))) == true ]]; then
board[$(( (y-1) * 3 + x ))]="${player}"
((move_count++))
win=$(check_win $player)
if [[ ${win} == "true" ]]; then
draw_board
echo -e "\n\t*** ${player} wins Tic-Tac-Toe ***"
break
else
if [[ ${player} == "X" ]]; then
player="O"
else
player="X"
fi
fi
else
echo -e "\t\t That space is taken.  Please choose another ..."
fi
fi
draw_board
done
if [[ ${win} == "false" ]]; then
echo -e "\n\t*** Draw ***"
fi
}
game_loop

huangapple
  • 本文由 发表于 2023年2月27日 19:17:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/75579778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定