Links und Funktionen
Sprachumschaltung

Navigationspfad
Sie sind hier: Startseite / Lehre / SS 2012 / Funktionale Programmierung / Übungen / Übung 04


Inhaltsbereich

Übung 04

Übungsblatt 04: GTK-binding

Haskell source code icon 04Gtk.hs — Haskell source code, 2 KB (2555 bytes)

Dateiinhalt

-- | Functional Programming, course at LMU, summer term 2012
--   Andreas Abel and Steffen Jost
--
-- Exercise sheet 4, 2012-05-15
--
-- Due: Monday 28 May 2012 before the lecture

----------------------------------------------------------------------
-- Exercise 1  Tic tac toc (2 players)
----------------------------------------------------------------------

-- Implement Tic tac toc for two players with a graphical user interface.
--
-- The players can click alternatingly on the board, which
-- displays X and O accordingly.
--
-- You can use buttons as in the lecture or something fancier.
--
-- Here is a template:

import Control.Monad

import Graphics.UI.Gtk

uiDecl = "\
  \<ui>\
  \  <menubar>\
  \    <menu action=\"FILE_MENU\">\
  \      <menuitem action=\"QUIT\"/>\
  \    </menu>\
  \  </menubar>\
  \  <toolbar>\
  \    <toolitem action=\"QUIT\"/>\
  \  </toolbar>\
  \</ui>"

createMenu :: VBox -> IO () -> IO ()
createMenu box quit = do

  -- create actions
  actionFileMenu <- actionNew "FILE_MENU" "File" Nothing Nothing
  actionQuit <- actionNew "QUIT" "Quit"
                  (Just "Exit Tic Tac Toe") (Just stockQuit)
  onActionActivate actionQuit quit

  -- create action group
  actionGroup <- actionGroupNew "ACTION_GROUP"
  forM_  [actionFileMenu,actionQuit] $ actionGroupAddAction actionGroup

  -- link action group to xml menu structure
  ui <- uiManagerNew
  uiManagerAddUiFromString ui uiDecl
  uiManagerInsertActionGroup ui actionGroup 0

  -- extract the menu
  Just menubar <- uiManagerGetWidget ui "/ui/menubar"
  boxPackStart box menubar PackNatural 0

  -- extract the toolbar
  Just toolbar <- uiManagerGetWidget ui "/ui/toolbar"
  boxPackStart box toolbar PackNatural 0

main :: IO ()
main = do
  initGUI

  -- set up main window
  window <- windowNew
  mainBox <- vBoxNew False 0
  set window
    [ windowTitle := "Tic tac toe"
    , windowDefaultWidth := 200
    , windowDefaultHeight := 150
    , containerChild := mainBox
    ]
  onDestroy window mainQuit

  -- add menu
  createMenu mainBox (widgetDestroy window)

  -- run
  widgetShowAll window
  mainGUI

----------------------------------------------------------------------
-- Exercise 2  Tic tac toc (1 player against computer)
----------------------------------------------------------------------

-- Modify your solution to Ex. 1 to enable playing against a computer.
--
-- The computer does not need to do smart moves.  It could play random
-- moves, if you want.  But you can also implement a smart player...

-- Submit both programs via uniWorX

Artikelaktionen


Funktionsleiste