[Gtk-sharp-list] MsgBox(2)
Daniel Campos
danielcampos@netcourrier.com
18 Apr 2003 13:37:45 -0400
--=-GVK+rl4bgd7LxxiEyx0T
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
Oops, well, Last day I sent a "Message Box", but
thanks to the people that told me that it already
exists in MessageDialog... I think I have to wash my glasses
before looking into sources... :)
Well,now I send a modified wersion of the sample I sent
last day, using Dialog, if you want to include it into
"samples".
It shows how to use MesageDialog,Fixed widget, and ModifyFg...
By the way, hoy can I change the text color of a Button (MofifyFg
and ModifyText doesn't seems to work for this)
Thanks
--
Daniel Campos <danielcampos@netcourrier.com>
--=-GVK+rl4bgd7LxxiEyx0T
Content-Disposition: attachment; filename=fungame.cs
Content-Type: text/plain; name=fungame.cs; charset=ISO-8859-15
Content-Transfer-Encoding: 7bit
// FunGame.cs - A sample of a funny game using Gtk#
// It shows how to use MsgBox and a Fixed Widget
//
// Author: Daniel Campos <danielcampos@netcourrier.com>
//
// (c) 2003 Daniel Campos
namespace GtkSamples
{
using Gdk;
using Gtk;
using GtkSharp;
using System;
using System.Drawing;
public class FunGame
{
static Gtk.Window MyWin;
static Fixed MyFix;
static Button[] Btns=new Button[9];
static int[,] Actual=new int[3,3];
static int[,] Possible=new int[3,3];
public static int Main()
{
Gdk.Color MyBlue=new Gdk.Color(0,0,255);
Application.Init();
MyWin=new Gtk.Window("Funny Game");
MyFix=new Fixed();
MyWin.SetPosition(WindowPosition.Center);
MyWin.DefaultSize = new Size (225, 225);
MyWin.DeleteEvent += new DeleteEventHandler (Window_Delete);
for (int myloop=0;myloop<9;myloop++)
{
Btns[myloop]=new Button("");
Btns[myloop].SetSizeRequest(75,75);
Btns[myloop].Clicked+= new EventHandler (btn_click);
Btns[myloop].ModifyBg(StateType.Prelight,MyBlue);
MyFix.Add(Btns[myloop]);
}
int counter=0;
for (int y=0;y<3;y++)
for (int x=0;x<3;x++)
MyFix.Move(Btns[counter++],x*75,y*75);
MyWin.Add(MyFix);
MyWin.ShowAll();
Application.Run();
return 0;
}
static void Refresh_Table()
{
int X;
int Y;
for(Y = 0;Y<3;Y++)
for( X = 0;X<3;X++)
switch(Actual[Y,X])
{
case 0:
Btns[X + Y * 3].Label = "";break;
case 1:
Btns[X + Y * 3].Label = "0";break;
case 2:
Btns[X + Y * 3].Label = "X";break;
}
}
static void btn_click (object obj, EventArgs args)
{
int A = 0;
int X = 0;
int Y = 0;
for (int myloop=0;myloop<9;myloop++)
if (obj==Btns[myloop])
A=myloop;
X=A;
while (!( X < 3 ))
{
X = X - 3;
Y = Y + 1;
}
if (Actual[Y,X]==0)
{
Btns[A].Label = "X";
Actual[Y,X] = 2;
Play();
}
}
static int Evaluate_End_of_Game(int Player)
{
int Y=0;
int X=0;
int Value=0;
int Aux=0;
Value = -1;
for( X = 0;X<3;X++)
for (Y = 0;Y < 3; Y++)
if (Actual[Y,X]== 0) Value = 0;
for (Y = 0;Y < 3; Y++)
{
Aux = 0;
for( X = 0;X<3;X++)
if (Actual[Y,X] == Player) Aux = Aux + 1;
if (Aux == 3) Value = 1;
}
for( X = 0;X<3;X++)
{
Aux = 0;
for (Y = 0;Y < 3; Y++)
if (Actual[Y,X] == Player) Aux = Aux + 1;
if (Aux == 3) Value = 1;
}
Aux = 0;
X = 0;
for (Y = 0;Y < 3; Y++)
if (Actual[Y,X++] == Player) Aux = Aux + 1;
if (Aux == 3) Value = 1;
Aux = 0;
X = 2;
for (Y = 0;Y < 3; Y++)
if (Actual[Y,X--] == Player) Aux = Aux + 1;
if (Aux == 3) Value = 1;
return Value;
}
static int Evaluate(int PosX,int PosY,int f1,int f2)
{
int X=0;
int Y=0;
int Value=0;
int CouldBe=1;
int Danger=0;
for (X = 0; X<3;X++)
{
if (Actual[PosY,X] == f2 ) Danger++;
if (Actual[PosY,X] == f1 ) CouldBe++;
}
if (Danger > 1) Danger*=2;
if (CouldBe > 1) CouldBe*=2;
if (CouldBe == 1)
Value = Danger;
else
Value = 0;
Danger=0;
CouldBe=1;
for (Y = 0; Y<3;Y++)
{
if (Actual[Y,PosX]==f2) Danger++;
if (Actual[Y,PosX]==f1) CouldBe=0;
}
if (Danger > 1) Danger*=2;
if (CouldBe > 1) CouldBe*=2;
if (CouldBe==1) Value = Value + Danger;
if (PosX == PosY)
{
Y = 0;
CouldBe = 1;
Danger = 0;
for (X = 0; X<3;X++)
{
if (Actual[Y,X]==f2) Danger++;
if (Actual[Y,X]==f1) CouldBe=0;
Y++;
}
if (Danger > 1) Danger*=2;
if (CouldBe > 1) CouldBe*=2;
if (CouldBe==1) Value = Value + Danger;
}
if ( (2 - PosX) == PosY )
{
Y = 2;
CouldBe = 1;
Danger = 0;
for (X = 0; X<3;X++)
{
if (Actual[Y,X]==f2) Danger++;
if (Actual[Y,X]==f1) CouldBe=0;
Y--;
}
if (Danger > 1) Danger*=2;
if (CouldBe > 1) CouldBe*=2;
if (CouldBe==1) Value = Value + Danger;
}
return Value;
}
static void ComputerPlays()
{
int X=0;
int Y=0;
int X_Ord=0;
int Y_Ord=0;
int P=0;
Possible=new int[3,3];
// defend
for( Y = 0; Y<3;Y++)
{
for( X = 0; X<3;X++)
{
if (Actual[Y,X]==0)
Possible[Y,X] = Evaluate(X, Y, 2, 1);
}
}
// attack
for( Y = 0; Y<3;Y++)
{
for( X = 0; X<3;X++)
{
if (Actual[Y,X]==0)
{
P = Evaluate(X, Y, 1, 2);
if (P > 1) P = P * 10;
Possible[Y,X] = Possible[Y,X] + P;
}
}
}
for( X = 0; X<3;X++)
{
for (Y=0;Y<3;Y++)
{
if (Actual[Y,X]==0)
{
X_Ord = X;
Y_Ord = Y;
}
}
}
for( X = 0; X<3;X++)
{
for (Y=0;Y<3;Y++)
{
if ( (Actual[Y,X]== 0) && (Possible[Y,X] > Possible[Y_Ord,X_Ord]) )
{
X_Ord = X;
Y_Ord = Y;
}
}
}
if (Actual[1,1] != 0)
Actual[Y_Ord,X_Ord] = 1;
else
Actual[1,1] = 1;
}
static void Play()
{
int Status=0;
int Resp=0;
MessageDialog Dlg;
Status = Evaluate_End_of_Game(2);
switch(Status)
{
case 0:
ComputerPlays();
Refresh_Table();
Status = Evaluate_End_of_Game(1);
switch(Status)
{
case 0: break;
case 1:
MessageVictory(1);break;
case 2:
MessageVictory(0);break;
}
break;
case 1:
MessageVictory(2);break;
case -1:
MessageVictory(0);break;
}
if (Status != 0)
{
Dlg=new MessageDialog(MyWin,DialogFlags.Modal,MessageType.Warning,ButtonsType.OkCancel,"Let's Play Again!");
Resp=Dlg.Run();
Dlg.Destroy();
if (Resp == -6)
Application.Quit();
else
{
Dlg=new MessageDialog(MyWin,DialogFlags.Modal,MessageType.Question,ButtonsType.YesNo,"Do you want to play first?");
Resp=Dlg.Run();
Dlg.Destroy();
Console.WriteLine(Resp);
Actual=new int[3,3];
if (Resp == -9 ) ComputerPlays();
Refresh_Table();
}
}
}
static void MessageVictory(int whowins)
{
MessageDialog Dlg;
switch(whowins)
{
case 0:
Dlg=new MessageDialog(MyWin,DialogFlags.Modal,MessageType.Info,ButtonsType.Ok,"Nobody Wins!");
Dlg.Run();
Dlg.Destroy();
break;
case 1:
Dlg=new MessageDialog(MyWin,DialogFlags.Modal,MessageType.Info,ButtonsType.Ok,"I Won!");
Dlg.Run();
Dlg.Destroy();
break;
case 2:
Dlg=new MessageDialog(MyWin,DialogFlags.Modal,MessageType.Info,ButtonsType.Ok,"You Won! How it is possible?");
Dlg.Run();
Dlg.Destroy();
break;
}
}
static void Window_Delete (object obj, DeleteEventArgs args)
{
Console.WriteLine("Bye,bye, may be you'll win another day!");
Application.Quit ();
args.RetVal = true;
}
}
}
--=-GVK+rl4bgd7LxxiEyx0T--