Tic-Tac-Toe

Dieses Thema im Forum "Fun" wurde erstellt von Gast123, 23.07.2013.

  1. #1 Gast123, 23.07.2013
    Gast123

    Gast123 Guest

    Mir war gerade langweilig... :D
    Code:
    #! /bin/bash
    
    declare -u A1=" " A2=" " A3=" " B1=" " B2=" " B3=" " C1=" " C2=" " C3=" "
    declare PLAYER1=X PLAYER2=O TURN=$PLAYER2 WINNER=
    declare CREDITS="Tic-Tac-Toe game
    
    (C) 2013 Richard Neumann <mail@richard-neumann.de>
    "
    
    
    print_game_map() {
    	/bin/echo "   A B C
    
     1 $A1|$B1|$C1
       -----
     2 $A2|$B2|$C2
       -----
     3 $A3|$B3|$C3"
    }
    
    check_win() {
    	if [ "$A1" != " " -a "$A1" == "$A2" -a "$A2" == "$A3" ]; then
    		WINNER=$A1
    		return 0
    	elif [ "$B1" != " " -a "$B1" == "$B2" -a "$B2" == "$B3" ]; then
    		WINNER=$B1
    		return 0
    	elif [ "$C1" != " " -a "$C1" == "$C2" -a "$C2" == "$C3" ]; then
    		WINNER=$C1
    		return 0
    	elif [ "$A1" != " " -a "$A1" == "$B1" -a "$B1" == "$C1" ]; then
    		WINNER=$B1
    		return 0
    	elif [ "$A2" != " " -a "$A2" == "$B2" -a "$B2" == "$C2" ]; then
    		WINNER=$A2
    		return 0
    	elif [ "$A3" != " " -a "$A3" == "$B3" -a "$B3" == "$C3" ]; then
    		WINNER=$A3
    		return 0
    	elif [ "$A1" != " " -a "$A1" == "$B2" -a "$B2" == "$C3" ]; then
    		WINNER=$A1
    		return 0
    	elif [ "$C1" != " " -a "$C1" == "$B2" -a "$B2" == "$A3" ]; then
    		WINNER=$C1
    		return 0
    	else
    		return 1
    	fi
    }
    
    prompt_turn() {
    	local -u FIELD
    
    	echo -n "Player $TURN: "
    	read FIELD
    	
    	case $FIELD in
    		A1)	if [ "$A1" == " " ]; then
    				A1=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		B1)	if [ "$B1" == " " ]; then
    				B1=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		C1)	if [ "$C1" == " " ]; then
    				C1=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		A2)	if [ "$A2" == " " ]; then
    				A2=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		B2)	if [ "$B2" == " " ]; then
    				B2=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		C2)	if [ "$C2" == " " ]; then
    				C2=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		A3)	if [ "$A3" == " " ]; then
    				A3=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		B3)	if [ "$B3" == " " ]; then
    				B3=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		C3)	if [ "$C3" == " " ]; then
    				C3=$TURN
    			else
    				prompt_turn
    			fi
    		;;
    		*)	prompt_turn
    	esac
    }
    
    ask_turn() {
    	clear
    	print_game_map
    
    	if [ "$TURN" == "$PLAYER1" ]; then
    		TURN=$PLAYER2
    		prompt_turn
    	else
    		TURN=$PLAYER1
    		prompt_turn
    	fi
    
    	check_win && { echo "Player $WINNER has won the game!"; exit; }
    
    	ask_turn
    }
    
    echo "$CREDITS"
    sleep 5
    ask_turn
    
     
  2. Anzeige

    Schau dir mal diese Kategorie an. Dort findest du bestimmt etwas.
    Registrieren bzw. einloggen, um diese und auch andere Anzeigen zu deaktivieren
  3. #2 bytepool, 24.07.2013
    bytepool

    bytepool Code Monkey

    Dabei seit:
    12.07.2003
    Beiträge:
    791
    Zustimmungen:
    0
    Ort:
    /home/sweden/göteborg
    Haha, nice!

    Dir fehlt noch check_draw:
    Code:
    check_draw() {
        if [ "$A1" != " " -a "$A2" != " " -a "$A3" != " "  -a "$B1" != " "  -a "$B2" != " "  -a "$B3" != " "  -a "$C1" != " "  -a "$C21" != " "  -a "$C3" != " " ]; then 
    	return 0
        else 
    	return 1
        fi
    }
    
    [...]
    
    check_draw && { print_board; echo "A draw has been reached!"; exit; }
    
    Und persoenlich gefaellt mir das board so besser: ;)
    Code:
    print_board() {
        /bin/echo "
       | A | B | C |
     1 | $A1 | $B1 | $C1 |
     2 | $A2 | $B2 | $C2 |
     3 | $A3 | $B3 | $C3 |
    "
    }
    
    MfG,
    bytepool
     
  4. #3 Gast123, 24.07.2013
    Gast123

    Gast123 Guest

    OMG, das habe ich völlig vergessen. :D
    Dabei ist es der häufigste Spielausgang - jedenfalls wenn man nicht betrunken ist. :)

    Danke!
     
Thema:

Tic-Tac-Toe