examples/declarative/toys/tic-tac-toe/tic-tac-toe.qml
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
    40 
    40 
    41 import Qt 4.7
    41 import Qt 4.7
    42 import "content"
    42 import "content"
    43 import "content/tic-tac-toe.js" as Logic
    43 import "content/tic-tac-toe.js" as Logic
    44 
    44 
    45 Item {
    45 Rectangle {
    46     id: game
    46     id: game
    47 
    47 
    48     property bool show: false
    48     property bool running: true
    49     property real difficulty: 1.0   //chance it will actually think
    49     property real difficulty: 1.0   //chance it will actually think
    50 
    50 
    51     width: 440
    51     width: display.width; height: display.height + 10
    52     height: 480
       
    53     anchors.fill: parent
       
    54 
    52 
    55     Image {
    53     Image {
    56         id: boardimage
    54         id: boardImage
    57         anchors { verticalCenter: parent.verticalCenter; horizontalCenter: parent.horizontalCenter }
       
    58         source: "content/pics/board.png"
    55         source: "content/pics/board.png"
    59     }
    56     }
    60 
    57 
    61     Grid {
    58     Text {
    62         id: board
    59         id: messageDisplay
    63         anchors.fill: boardimage
    60         anchors.centerIn: parent
    64         columns: 3
    61         color: "blue"
       
    62         style: Text.Outline; styleColor: "white"
       
    63         font.pixelSize: 50; font.bold: true
       
    64         visible: false
    65 
    65 
    66         Repeater {
    66         Timer {
    67             model: 9
    67             running: messageDisplay.visible
    68             TicTac {
    68             onTriggered: { 
    69                 width: board.width/3
    69                 messageDisplay.visible = false;
    70                 height: board.height/3
    70                 Logic.restartGame();
    71                 onClicked: {
    71             }
    72                     if (!endtimer.running) {
    72         }
    73                         if (!Logic.makeMove(index,"X"))
    73     }
    74                             Logic.computerTurn()
    74 
       
    75     Column {
       
    76         id: display
       
    77 
       
    78         Grid {
       
    79             id: board
       
    80             width: boardImage.width; height: boardImage.height
       
    81             columns: 3
       
    82 
       
    83             Repeater {
       
    84                 model: 9
       
    85 
       
    86                 TicTac {
       
    87                     width: board.width/3
       
    88                     height: board.height/3
       
    89 
       
    90                     onClicked: {
       
    91                         if (game.running && Logic.canPlayAtPos(index)) {
       
    92                             if (!Logic.makeMove(index, "X"))
       
    93                                 Logic.computerTurn();
       
    94                         }
    75                     }
    95                     }
    76                 }
    96                 }
    77             }
    97             }
    78         }
    98         }
    79 
    99 
    80         Timer {
   100         Row {
    81             id: endtimer
   101             spacing: 4
    82             interval: 1600
   102             anchors.horizontalCenter: parent.horizontalCenter
    83             onTriggered: { msg.opacity = 0; Logic.restart() }
   103 
       
   104             Button {
       
   105                 text: "Hard"
       
   106                 pressed: game.difficulty == 1.0
       
   107                 onClicked: { game.difficulty = 1.0 }
       
   108             }
       
   109             Button {
       
   110                 text: "Moderate"
       
   111                 pressed: game.difficulty == 0.8
       
   112                 onClicked: { game.difficulty = 0.8 }
       
   113             }
       
   114             Button {
       
   115                 text: "Easy"
       
   116                 pressed: game.difficulty == 0.2
       
   117                 onClicked: { game.difficulty = 0.2 }
       
   118             }
    84         }
   119         }
    85     }
   120     }
    86 
       
    87     Row {
       
    88         spacing: 4
       
    89         anchors { top: board.bottom; horizontalCenter: board.horizontalCenter }
       
    90 
       
    91         Button {
       
    92             text: "Hard"
       
    93             onClicked: game.difficulty = 1.0;
       
    94             down: game.difficulty == 1.0
       
    95         }
       
    96         Button {
       
    97             text: "Moderate"
       
    98             onClicked: game.difficulty = 0.8;
       
    99             down: game.difficulty == 0.8
       
   100         }
       
   101         Button {
       
   102             text: "Easy"
       
   103             onClicked: game.difficulty = 0.2;
       
   104             down: game.difficulty == 0.2
       
   105         }
       
   106     }
       
   107 
       
   108     Text {
       
   109         id: msg
       
   110 
       
   111         anchors.centerIn: parent
       
   112         opacity: 0
       
   113         color: "blue"
       
   114         style: Text.Outline; styleColor: "white"
       
   115         font.pixelSize: 50; font.bold: true
       
   116     }
       
   117 }
   121 }