<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Compiled Thoughts by John Ciacia &#187; gtk</title>
	<atom:link href="http://www.johnciacia.com/tag/gtk/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johnciacia.com</link>
	<description>Science, Technology, and Beyond</description>
	<lastBuildDate>Fri, 06 Jan 2012 15:46:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Tic Tac Toe</title>
		<link>http://www.johnciacia.com/2009/05/22/tic-tac-toe/</link>
		<comments>http://www.johnciacia.com/2009/05/22/tic-tac-toe/#comments</comments>
		<pubDate>Sat, 23 May 2009 00:34:31 +0000</pubDate>
		<dc:creator>John</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Snippets]]></category>
		<category><![CDATA[gtk]]></category>
		<category><![CDATA[tic tac toe]]></category>

		<guid isPermaLink="false">http://www.johnciacia.com/?p=163</guid>
		<description><![CDATA[A graphical Tic Tac Toe board using PHP-Gtk [code]#!/usr/local/bin/php -q]]></description>
			<content:encoded><![CDATA[<p>A graphical Tic Tac Toe board using PHP-Gtk<br />
[code]#!/usr/local/bin/php -q<br />
<?php<br />
class TicTacToe extends GtkWindow{</p>
<p>	private $_buttons;<br />
	private $_dialog;<br />
	private $_count;<br />
	private $_menu;<br />
	private $_lbl;</p>
<p>	public function __construct()<br />
	{<br />
		parent::__construct();</p>
<p>		$this->set_title('Tic-Tac-Toe');<br />
		$this->set_size_request(250, 250);<br />
		$this->set_position(Gtk::WIN_POS_CENTER);<br />
		$this->connect_simple('destroy', array('gtk', 'main_quit'));</p>
<p>		#Create the Buttons and add them to the table<br />
		for($i = 0; $i < 9; $i++) {<br />
		    $this->_buttons[$i] = new GtkButton('');<br />
		    $this->_buttons[$i]->connect_simple('clicked', array($this, 'clicked'), $i);<br />
		}</p>
<p>		#Create a GtkTable<br />
		$tbl = new GtkTable(3, 3);</p>
<p>		#Add nine buttons to the table<br />
		for($i = 0; $i < 9; $i++) {<br />
		    $tbl->attach($this->_buttons[$i], ($i%3), ($i%3)+1, floor($i/3), floor($i/3)+1);<br />
		}</p>
<p>		$this->buildMenu();</p>
<p>        #GtkStatusBar<br />
        $status = new GtkStatusbar();<br />
        $context = array('Not Connected', 'Connected');<br />
		$status->push($status->get_context_id($context[0]), $context[0]);</p>
<p>		#GtkVBox<br />
		$vbox = new GtkVBox();<br />
		#Add the GtkVBox to the main window<br />
		$this->add($vbox);<br />
		#Add the GtkMenu to the GtkVBox<br />
		$vbox->pack_start($this->_menu, false);<br />
		#Add the GtkTable to the GtkVBox<br />
		$vbox->pack_start($tbl, true, true);<br />
		#Add the GtkStatusBar to the GtkVBox<br />
		$vbox->pack_start($status, false);</p>
<p>		$this->show_all();</p>
<p>		$this->server();</p>
<p>	}</p>
<p>	private function server()<br />
	{<br />
        #Not implemented<br />
	}</p>
<p>	private function buildMenu()<br />
	{<br />
		#GtkMenu<br />
		$this->_menu = new GtkMenuBar();<br />
		$this->_menu->append($file_item = new GtkMenuItem('_File'));<br />
        $this->_menu->append($help_item = new GtkMenuItem('_Help'));</p>
<p>		$file_item->set_submenu($file_menu = new  GtkMenu());<br />
        $file_menu->append($file_new_item = new GtkMenuItem('_New'));<br />
        $file_menu->append($file_connect_item = new GtkMenuItem('_Connect'));<br />
        $file_menu->append($file_quit_item = new GtkMenuItem('_Quit'));</p>
<p> 		$help_item->set_submenu($help_menu = new  GtkMenu());<br />
		$help_menu->append($help_about_item = new GtkMenuItem('_About'));</p>
<p>        $file_new_item->connect_simple('activate', array($this,'restart'));<br />
        $help_about_item->connect_simple('activate', array($this,'about'));<br />
        $file_quit_item->connect_simple('activate', array('Gtk','main_quit'));<br />
	}</p>
<p>	public function about()<br />
	{<br />
		$about = new GtkAboutDialog();<br />
		$about->set_authors(array("John Ciacia"));<br />
		$about->set_comments("Tic-Tac-Toe created using the PHP-Gtk extension.");<br />
		$about->set_copyright("Copyright (C) 2008 John Ciacia");<br />
		$about->set_license("This software is released under the GNU GPL");<br />
		$about->set_version("1.0");<br />
		$about->set_website("http://www.codecall.net");<br />
		$about->set_website_label("http://www.codecall.net");<br />
		$about->show_all();<br />
	}</p>
<p>	public function clicked($i)<br />
	{<br />
		!isset($this->_count) ? $this->_count = 0 : $this->_count++;<br />
		($this->_count % 2 == 0) ? $this->_lbl = 'X' : $this->_lbl = 'O';</p>
<p>		$this->_buttons[$i]->set_label($this->_lbl);<br />
        $this->_buttons[$i]->set_sensitive(false);</p>
<p>        $this->_check();<br />
	}</p>
<p>	private function _check()<br />
	{<br />
	    $win = false;</p>
<p>        $combinations = array(<br />
            0 => array(0, 1, 2),<br />
            1 => array(3, 4, 5),<br />
            2 => array(6, 7, 8),</p>
<p>            3 => array(0, 3, 6),<br />
            4 => array(1, 4, 7),<br />
            5 => array(2, 5, 8),</p>
<p>            6 => array(0, 4, 8),<br />
            7 => array(2, 4, 6));</p>
<p>        for($i = 0; $i < 8; $i++)<br />
        {<br />
            if( ($this->_buttons[$combinations[$i][0]]->get_label() ==<br />
                    $this->_buttons[$combinations[$i][1]]->get_label()) &#038;&#038; </p>
<p>                ($this->_buttons[$combinations[$i][1]]->get_label() ==<br />
                    $this->_buttons[$combinations[$i][2]]->get_label()) &#038;&#038; </p>
<p>                ($this->_buttons[$combinations[$i][0]]->get_label() != "")){</p>
<p>                $win = true;<br />
            }<br />
        }</p>
<p>	    if($win) {<br />
	        $this->end_dialog(true);<br />
	    }<br />
	    else if(!$win &#038;&#038; $this->_count == <img src='http://www.johnciacia.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> {<br />
	        $this->end_dialog();<br />
	    }<br />
	    else {<br />
	    	;<br />
	    }<br />
	}</p>
<p>	private function end_dialog($win = false){</p>
<p>		$this->_dialog = new GtkDialog(null, $this);<br />
		$this->_dialog->set_title("Play Again?");<br />
		$this->_dialog->set_default_size('100','100');<br />
		$this->_dialog->set_modal(true);<br />
		$this->_dialog->set_transient_for($this);<br />
		$this->_dialog->set_resizable(false);<br />
		$this->_dialog->connect_simple('destroy', array($this, 'destroy'));<br />
		if($win) {<br />
		    $label = new GtkLabel($this->_lbl . " has won the game. Play again?");<br />
		} else {<br />
			$label = new GtkLabel("It's a tie! Play again?");<br />
		}<br />
		$button_yes = new GtkButton('_Yes');<br />
		$button_yes->connect_simple('clicked', array($this, 'restart'));<br />
		$button_no = new GtkButton('_No');<br />
		$button_no->connect('clicked', array($this, 'destroy'));</p>
<p>		$vbox1 = $this->_dialog->vbox;<br />
		$vbox2 = $this->_dialog->action_area;<br />
		$vbox1->pack_start($label);<br />
		$vbox2->pack_start($button_yes);<br />
		$vbox2->pack_start($button_no);<br />
		$this->_dialog->show_all();<br />
	}</p>
<p>	public function restart()<br />
	{<br />
		if(isset($this->_dialog)) { $this->_dialog->hide_all(); }<br />
		unset($this->_count);</p>
<p>		foreach($this->_buttons as $button):<br />
			$button->set_label('');<br />
			$button->set_sensitive(true);<br />
		endforeach;<br />
	}</p>
<p>    public function destroy()<br />
    {<br />
    	echo "Good Bye!\n";<br />
        Gtk::main_quit();<br />
    }</p>
<p>}</p>
<p>new TicTacToe();<br />
Gtk::main();</p>
<p>?>[/code]</p>
<p><a href="http://www.johnciacia.com/wp-content/uploads/2009/05/tictactoe.png"><img src="http://www.johnciacia.com/wp-content/uploads/2009/05/tictactoe-300x187.png" alt="tictactoe" title="tictactoe" width="300" height="187" class="aligncenter size-medium wp-image-164" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.johnciacia.com/2009/05/22/tic-tac-toe/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using disk: enhanced

Served from: www.johnciacia.com @ 2012-02-05 10:32:06 -->
