Click to See Complete Forum and Search --> : fubar


petes1234
January 28th, 2007, 11:16 AM
fubar

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace foo5
{
public partial class Form6 : Form
{
#region constants
const float LineThicknessRatio = 0.05f;
// thickness / (1/3 of line segment)
// 1.0 obliterates it
const int TTTPictBoxSize = 450;
const float TTTGridMarginRatio = 0.05f;
// if 0.5 then it takes up whole picturebox
private const float lineLength = TTTPictBoxSize * (1 - 2 * TTTGridMarginRatio);
private const float XOCharSize = 0.8f* (lineLength * (1 - LineThicknessRatio)) / 3;
#endregion

enum CellState { Empty, X, O };

#region Private Variables
private Bitmap TicTacToeBitMap;
private Form6c myForm6c;
private GridCell[,] myGrid = new GridCell[3, 3];
#endregion

#region Public Methods
public Form6()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
float myXLoc = (TTTPictBoxSize - lineLength) / 3.0f +
// the margin
//(lineLength / 6) +
(i * lineLength / 3);
float myYLoc = (TTTPictBoxSize - lineLength) / 3.0f +
//(lineLength / 6) +
(j * lineLength / 3);
myGrid[i, j] = new GridCell(new PointF(myXLoc, myYLoc), this);
}
}
}

public void ClearGrid()
{
foreach (GridCell myGC in myGrid)
{
myGC.myCellState = CellState.Empty;
}
}

public void PlaceX(Point XLocation)
{
myGrid[XLocation.X, XLocation.Y].myCellState = CellState.X;
}

public void PlaceO(Point OLocation)
{
myGrid[OLocation.X, OLocation.Y].myCellState = CellState.O;
}

#endregion

private class GridCell
{
private CellState _myCellState;

public CellState myCellState
{
get { return _myCellState; }
set
{
_myCellState = value;
PlaceCharInCell();
}
}

private void PlaceCharInCell()
{
string myStr = string.Empty;
if (myCellState != CellState.Empty)
{
if (myCellState == CellState.O)
{
myStr = "O";
}
else
{
myStr = "X";
}
ParentForm.myDrawChar(myStr, Color.Red, XOCharSize, myLocation);
}
}

private PointF _myLocation;

public PointF myLocation
{
get { return _myLocation; }
set { _myLocation = value; }
}

private float _XSize;

public float XSize
{
get { return _XSize; }
set { _XSize = value; }
}
private float _YSize;

public float YSize
{
get { return _YSize; }
set { _YSize = value; }
}
public Form6 ParentForm;
//!!!!!!!!!!!!!! may cause bug if form name changed!

public GridCell(PointF GridCellLoc, Form6 PForm)
{
myLocation = GridCellLoc;
XSize = XOCharSize;
ParentForm = PForm;
}
}
#region Form Events
private void Form6_Load(object sender, EventArgs e)
{
TTTPicBox.Size = new Size(TTTPictBoxSize, TTTPictBoxSize);
TicTacToeBitMap = new Bitmap(
TTTPicBox.Width,
TTTPicBox.Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
InitializeMyBitmap();
CreateTTTGrid();

myGrid[0, 0].myCellState = CellState.X;
myGrid[1, 1].myCellState = CellState.X;
myGrid[2, 2].myCellState = CellState.X;
myGrid[0, 2].myCellState = CellState.O;
myGrid[1, 2].myCellState = CellState.O;
myGrid[2, 0].myCellState = CellState.O;
}

private void TTTPicBox_Paint(object sender, PaintEventArgs e)
{
Graphics oGraphics = e.Graphics;
oGraphics.DrawImage(TicTacToeBitMap, 0, 0);
}

private void TTTPicBox_Click(object sender, EventArgs e)
{

}

private void Form6_FormClosed(object sender, FormClosedEventArgs e)
{
TicTacToeBitMap.Dispose();
myForm6c.Close();
}
#endregion

#region Private Non-Graphic Methods
private void CreateTTTGrid()
{
CreateColumns(lineLength);
CreateRows(lineLength);
}

private void CreateColumns(float lineLength)
{
float YStart = (TTTPictBoxSize - lineLength) / 2.0f;
float YEnd = TTTPictBoxSize - YStart;
float XStart = YStart;
float LineThickness = lineLength * LineThicknessRatio/3;
for (int i = 1; i < 3; i++)
{
myDrawLine(
Color.Blue,
LineThickness,
XStart + i * lineLength / 3, YStart,
XStart + i * lineLength / 3, YEnd);
}

}

private void CreateRows(float lineLength)
{
float XStart = (TTTPictBoxSize - lineLength) / 2.0f;
float XEnd = TTTPictBoxSize - XStart;
float YStart = XStart;
float LineThickness = lineLength*LineThicknessRatio/3;
for (int i = 1; i < 3; i++)
{
myDrawLine(
Color.Blue,
LineThickness,
XStart, YStart + i * lineLength / 3,
XEnd, YStart + i * lineLength / 3);
}

}

public void setForm6c(Form6c myF6c)
{
myForm6c = myF6c;
}

#endregion

#region Private Graphic Methods
private void InitializeMyBitmap()
{
Graphics oGraphics = Graphics.FromImage(TicTacToeBitMap);
Pen myPen = new Pen(Color.AliceBlue);
for (int i = 0; i < TTTPicBox.Width; i++)
{
oGraphics.DrawLine(
myPen,
i, 0,
i, TTTPicBox.Height);
}
myPen.Dispose();
oGraphics.Dispose();
TTTPicBox.Invalidate();
}

private void myDrawLine(Color color, float LineThickness, float f1, float f2, float f3, float f4)
{
Pen myPen = new Pen(color);
myPen.Width = LineThickness;
Graphics myGraphics = Graphics.FromImage(TicTacToeBitMap);
myGraphics.DrawLine(myPen, f1, f2, f3, f4);
myGraphics.Dispose();
myPen.Dispose();
}

internal void myDrawChar(string myStr, Color myColor, float charSize, PointF StrLoc)
{
Graphics myGraphics = Graphics.FromImage(TicTacToeBitMap);
Font myFont = new Font("Arial", charSize);
SolidBrush myBrush = new SolidBrush(myColor);
myGraphics.DrawString(myStr, myFont, myBrush, StrLoc);
myFont.Dispose();
myBrush.Dispose();
myGraphics.Dispose();
}
#endregion

}
}

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace foo5
{
public partial class Form6 : Form
{
#region constants
const float LineThicknessRatio = 0.05f;
// thickness / (1/3 of line segment)
// 1.0 obliterates it
const int TTTPictBoxSize = 450;
const float TTTGridMarginRatio = 0.05f;
// if 0.5 then it takes up whole picturebox
private const float lineLength = TTTPictBoxSize * (1 - 2 * TTTGridMarginRatio);
private const float XOCharSize = 0.8f* (lineLength * (1 - LineThicknessRatio)) / 3;
#endregion

enum CellState { Empty, X, O };

#region Private Variables
private Bitmap TicTacToeBitMap;
private Form6c myForm6c;
private GridCell[,] myGrid = new GridCell[3, 3];
#endregion

#region Public Methods
public Form6()
{
InitializeComponent();
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
float myXLoc = (TTTPictBoxSize - lineLength) / 3.0f +
// the margin
//(lineLength / 6) +
(i * lineLength / 3);
float myYLoc = (TTTPictBoxSize - lineLength) / 3.0f +
//(lineLength / 6) +
(j * lineLength / 3);
myGrid[i, j] = new GridCell(new PointF(myXLoc, myYLoc), this);
}
}
}

public void ClearGrid()
{
foreach (GridCell myGC in myGrid)
{
myGC.myCellState = CellState.Empty;
}
}

public void PlaceX(Point XLocation)
{
myGrid[XLocation.X, XLocation.Y].myCellState = CellState.X;
}

public void PlaceO(Point OLocation)
{
myGrid[OLocation.X, OLocation.Y].myCellState = CellState.O;
}

#endregion

private class GridCell
{
private CellState _myCellState;

public CellState myCellState
{
get { return _myCellState; }
set
{
_myCellState = value;
PlaceCharInCell();
}
}

private void PlaceCharInCell()
{
string myStr = string.Empty;
if (myCellState != CellState.Empty)
{
if (myCellState == CellState.O)
{
myStr = "O";
}
else
{
myStr = "X";
}
ParentForm.myDrawChar(myStr, Color.Red, XOCharSize, myLocation);
}
}

private PointF _myLocation;

public PointF myLocation
{
get { return _myLocation; }
set { _myLocation = value; }
}

private float _XSize;

public float XSize
{
get { return _XSize; }
set { _XSize = value; }
}
private float _YSize;

public float YSize
{
get { return _YSize; }
set { _YSize = value; }
}
public Form6 ParentForm;
//!!!!!!!!!!!!!! may cause bug if form name changed!

public GridCell(PointF GridCellLoc, Form6 PForm)
{
myLocation = GridCellLoc;
XSize = XOCharSize;
ParentForm = PForm;
}
}
#region Form Events
private void Form6_Load(object sender, EventArgs e)
{
TTTPicBox.Size = new Size(TTTPictBoxSize, TTTPictBoxSize);
TicTacToeBitMap = new Bitmap(
TTTPicBox.Width,
TTTPicBox.Height,
System.Drawing.Imaging.PixelFormat.Format24bppRgb);
InitializeMyBitmap();
CreateTTTGrid();

myGrid[0, 0].myCellState = CellState.X;
myGrid[1, 1].myCellState = CellState.X;
myGrid[2, 2].myCellState = CellState.X;
myGrid[0, 2].myCellState = CellState.O;
myGrid[1, 2].myCellState = CellState.O;
myGrid[2, 0].myCellState = CellState.O;
}

private void TTTPicBox_Paint(object sender, PaintEventArgs e)
{
Graphics oGraphics = e.Graphics;
oGraphics.DrawImage(TicTacToeBitMap, 0, 0);
}

private void TTTPicBox_Click(object sender, EventArgs e)
{

}

private void Form6_FormClosed(object sender, FormClosedEventArgs e)
{
TicTacToeBitMap.Dispose();
myForm6c.Close();
}
#endregion

#region Private Non-Graphic Methods
private void CreateTTTGrid()
{
CreateColumns(lineLength);
CreateRows(lineLength);
}

private void CreateColumns(float lineLength)
{
float YStart = (TTTPictBoxSize - lineLength) / 2.0f;
float YEnd = TTTPictBoxSize - YStart;
float XStart = YStart;
float LineThickness = lineLength * LineThicknessRatio/3;
for (int i = 1; i < 3; i++)
{
myDrawLine(
Color.Blue,
LineThickness,
XStart + i * lineLength / 3, YStart,
XStart + i * lineLength / 3, YEnd);
}

}

private void CreateRows(float lineLength)
{
float XStart = (TTTPictBoxSize - lineLength) / 2.0f;
float XEnd = TTTPictBoxSize - XStart;
float YStart = XStart;
float LineThickness = lineLength*LineThicknessRatio/3;
for (int i = 1; i < 3; i++)
{
myDrawLine(
Color.Blue,
LineThickness,
XStart, YStart + i * lineLength / 3,
XEnd, YStart + i * lineLength / 3);
}

}

public void setForm6c(Form6c myF6c)
{
myForm6c = myF6c;
}

#endregion

#region Private Graphic Methods
private void InitializeMyBitmap()
{
Graphics oGraphics = Graphics.FromImage(TicTacToeBitMap);
Pen myPen = new Pen(Color.AliceBlue);
for (int i = 0; i < TTTPicBox.Width; i++)
{
oGraphics.DrawLine(
myPen,
i, 0,
i, TTTPicBox.Height);
}
myPen.Dispose();
oGraphics.Dispose();
TTTPicBox.Invalidate();
}

private void myDrawLine(Color color, float LineThickness, float f1, float f2, float f3, float f4)
{
Pen myPen = new Pen(color);
myPen.Width = LineThickness;
Graphics myGraphics = Graphics.FromImage(TicTacToeBitMap);
myGraphics.DrawLine(myPen, f1, f2, f3, f4);
myGraphics.Dispose();
myPen.Dispose();
}

internal void myDrawChar(string myStr, Color myColor, float charSize, PointF StrLoc)
{
Graphics myGraphics = Graphics.FromImage(TicTacToeBitMap);
Font myFont = new Font("Arial", charSize);
SolidBrush myBrush = new SolidBrush(myColor);
myGraphics.DrawString(myStr, myFont, myBrush, StrLoc);
myFont.Dispose();
myBrush.Dispose();
myGraphics.Dispose();
}
#endregion

}
}

petes1234
January 28th, 2007, 11:19 AM
this is a test
fubar3fubar
this is only a test


import java.util.Random;

enum RPS
{
ROCK("rock"),
PAPER("paper"),
SCISSORS("scissors");

private String text;
private static Random rand = new Random();
private final static int[][] PAYOFF_MATRIX =
{
{0, -1, 1}, // RR, RP, RS
{1, 0, -1}, // PR, PP, PS
{-1, 1, 0} // SR, SP, SS
};

private RPS(String text)
{
this.text = text;
}

public static int payoff(RPS r1, RPS r2)
{
int index1 = r1.ordinal();
int index2 = r2.ordinal();
return PAYOFF_MATRIX[index1][index2];
}


public String getText()
{
return this.text;
}

@Override
public String toString()
{
return getText();
}

public static RPS getRpsIgnoreCase(String text)
{
RPS[] rpsArray = RPS.values();
for (int i = 0; i < rpsArray.length; i++)
{
if (text.equalsIgnoreCase(rpsArray[i].getText()))
{
return rpsArray[i];
}
}
return null;
}

public static RPS getRandom()
{
int randInt = rand.nextInt(RPS.values().length);
return RPS.values()[randInt];
}

public static void main(String[] args)
{
for (int i = 0; i < RPS.values().length; i++)
{
System.out.println(RPS.values()[i]);
}

for (int i = 0; i < RPS.values().length; i++)
{
for (int j = 0; j < RPS.values().length; j++)
{
RPS rps1 = RPS.values()[i];
RPS rps2 = RPS.values()[j];

try
{
int result = RPS.payoff(rps1, rps2);
//rps1.myCompareTo(rps2);
System.out.println(rps1 + " compared to " + rps2 + " = " + result);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

}



public class RPSPlayer
{
private RPS rps;
private int score;

public static int payoff(RPSPlayer player1, RPSPlayer player2)
{
int result = RPS.payoff(player1.rps, player2.rps);
if (result > 0)
{
player1.incrScore();
}
else if (result < 0)
{
player2.incrScore();
}

return result;
}

public RPSPlayer()
{
rps = null;
score = 0;
}

public RPS getRps()
{
return rps;
}

public void setRps(RPS rps)
{
this.rps = rps;
}

public int getScore()
{
return score;
}

public void setScore(int score)
{
this.score = score;
}

public void incrScore()
{
score++;
}

@Override
public String toString()
{
return rps.toString();
}
}



import java.util.Scanner;

public class MyRPS
{
private RPSPlayer user;
private RPSPlayer comp;
private Scanner sc;
private RPS[] rpsAr = RPS.values();

public MyRPS()
{
user = new RPSPlayer();
comp = new RPSPlayer();
sc = new Scanner(System.in);
}

private void promptForChoice()
{
System.out.println("Please make a choice: ");
for (int i = 0; i < rpsAr.length; i++)
{
System.out.print(i + " for " + rpsAr[i] + "; ");
}
System.out.println(rpsAr.length + " to quit");
}

public int getChoice()
{
promptForChoice();
while (!sc.hasNextInt())
{
sc.nextLine();
}
return sc.nextInt();
}

public void mainLoop()
{
int choice = getChoice();
while (choice != rpsAr.length)
{
if (choice >= 0 && choice < rpsAr.length)
{
user.setRps(rpsAr[choice]);
comp.setRps(RPS.getRandom());
int result = RPSPlayer.payoff(user, comp);
System.out.print("User: " + user + "; Computer: " + comp + "; Result: ");

switch (result)
{
case 0:
System.out.println("Tie");
break;
case 1:
System.out.println("User Wins");
break;
case -1:
System.out.println("Computer Wins");
break;

default:
break;
}
System.out.println();
}
choice = getChoice();
}

System.out.println();
System.out.println("Game Over");
System.out.print("User Score: " + user.getScore() + ", ");
System.out.println("Computer Scre: " + comp.getScore());
System.out.print("Final Result: ");
if (user.getScore() > comp.getScore())
{
System.out.println("User Wins");
}
else if (user.getScore() < comp.getScore())
{
System.out.println("Computer Wins");
}
else
{
System.out.println("Tie");
}

}

public static void main(String[] args)
{
MyRPS myrps = new MyRPS();
myrps.mainLoop();

}

}