twisted reactor.spawnProcess get stdout w/o bufffering on windows
I'm running an external process and I need to get the stdout immediately
so I can push it to a textview, on GNU/Linux I can use "usePTY=True" to
get the stdout by line, unfortunately usePTY is not available on windows.
I'm fairly new to twisted, is there a way to achieve the same result on
Windows with some twisted (or python maybe) magic stuff?
Boothman
Thursday, 3 October 2013
Wednesday, 2 October 2013
loading data to my mainform
loading data to my mainform
studentrecords table.,.,contains IDNumber,LastName,Course,Firstname
i will login through my system using IDNumber and once i go to the
mainform iwant to display LASTname and course to the 2 textboxes in the
mainform using the IDNumber as their Primary Key.,.My code errors
Dim con As New OleDbConnection Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
'get connection string declared in the Module1.vb and assing it to conn
variable
con = New OleDbConnection(Get_Constring)
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.Text
sSQL = "SELECT LastName FROM studentsrecords where IDNumber like '%" &
studentslogin.[txtIDNumber].Text & "%' order by ID desc"
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
'evalutionrate.[lbllogin].Text = studentslogin.[txtIDNumber].Text
evalutionrate.[lbllogin].Text = sSQL
evalutionrate.Show()
studentrecords table.,.,contains IDNumber,LastName,Course,Firstname
i will login through my system using IDNumber and once i go to the
mainform iwant to display LASTname and course to the 2 textboxes in the
mainform using the IDNumber as their Primary Key.,.My code errors
Dim con As New OleDbConnection Dim cmd As New OleDbCommand
Dim da As New OleDbDataAdapter
Dim dt As New DataTable
Dim sSQL As String = String.Empty
'get connection string declared in the Module1.vb and assing it to conn
variable
con = New OleDbConnection(Get_Constring)
con.Open()
cmd.Connection = con
cmd.CommandType = CommandType.Text
sSQL = "SELECT LastName FROM studentsrecords where IDNumber like '%" &
studentslogin.[txtIDNumber].Text & "%' order by ID desc"
cmd.CommandText = sSQL
da.SelectCommand = cmd
da.Fill(dt)
'evalutionrate.[lbllogin].Text = studentslogin.[txtIDNumber].Text
evalutionrate.[lbllogin].Text = sSQL
evalutionrate.Show()
Amazon availability zones
Amazon availability zones
I'm fairly new to Amazon services and wondering what some of the best
practices are for clustering/load balancing?
I have a load balancer in my colo (NJ) which may potentially be upgraded
to Netscaler.
The application we're hosting on Amazon is nothing crazy and don't expect
too much traffic. We're looking at 2 linux instances that would run a Node
JS application with a MongoDB replica set. From what I understand, Amazon
will evenly divide the traffic amongst the zones. The end-users location
has no effect on where they'll be distributed (ie if I have a server in
the west coast and one in the east coast, the user in the east coast could
be directed to either east or west).
If I wanted to direct users traffic based on location, a global DNS
solution would make more sense?
One server would be the master db and the other would be slave with data
replicating to each other.
Anybody have any experience with this and how is the network performance?
A question about EC2/S3
EC2 Instances and S3 buckets can only communicate if they are in the same
region, correct?
I'm fairly new to Amazon services and wondering what some of the best
practices are for clustering/load balancing?
I have a load balancer in my colo (NJ) which may potentially be upgraded
to Netscaler.
The application we're hosting on Amazon is nothing crazy and don't expect
too much traffic. We're looking at 2 linux instances that would run a Node
JS application with a MongoDB replica set. From what I understand, Amazon
will evenly divide the traffic amongst the zones. The end-users location
has no effect on where they'll be distributed (ie if I have a server in
the west coast and one in the east coast, the user in the east coast could
be directed to either east or west).
If I wanted to direct users traffic based on location, a global DNS
solution would make more sense?
One server would be the master db and the other would be slave with data
replicating to each other.
Anybody have any experience with this and how is the network performance?
A question about EC2/S3
EC2 Instances and S3 buckets can only communicate if they are in the same
region, correct?
Fixed horizontal ScrollView above ListFragment
Fixed horizontal ScrollView above ListFragment
I am displaying a list using a custom ListFragment. The fragment inflates
a layout and works fine. I want to add a horizontal scrollView above the
fragment to act as a category menu. It must be fixed, so addHeaderView is
not acceptable.
I found a layout example here on SO and plugged it into my layout to how
it could be made to work. But in its current form below, it does what you
would expect - puts a horizontal scrollview in every entry on the list.
So how do I share a screen with a static view on top and the listfragment
below.
Here is the layout that does not work - as I mentioned the Scrollview part
is not mine and is only included as an example. The relativelayout at the
bottom makes the actual list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
<HorizontalScrollView
android:id="@+id/hor_svID"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:scrollbars="none"
android:fillViewport="false"
android:focusable="false" >
<!--android:background="@drawable/submenu_bg"> -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:focusable="false"
android:background="#FFFFFF"
android:gravity="center">
<TextView android:id="@+id/TechnologyTxtVId"
android:text="TECHNOLOGY"
android:textColor="#342D7E"
android:textSize="12sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/SportsTxtVId"
android:text="SPORTS"
android:textColor="#342D7E"
android:textStyle="bold"
android:textSize="12sp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="15dip"></TextView>
<TextView android:id="@+id/EntntTxtVId"
android:text="ENTERTAINMENT"
android:textStyle="bold"
android:paddingLeft="15dip"
android:textSize="12sp"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/LocalTxtVId"
android:text="LOCAL"
android:textStyle="bold"
android:paddingLeft="15dip"
android:textSize="12sp"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/WorldTxtVId"
android:text="WORLD"
android:textStyle="bold"
android:textSize="12sp"
android:paddingLeft="15dip"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/FeaturesTxtVId"
android:text="FEATURES"
android:textStyle="bold"
android:textSize="12sp"
android:paddingLeft="15dip"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/RecentTxtVId"
android:text="RECENT"
android:textStyle="bold"
android:textSize="12sp"
android:paddingLeft="15dip"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
<ImageView
android:id="@+id/imageView_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="@dimen/default_padding"
android:contentDescription="@null"
android:focusable="false"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView_thumbnail"
android:layout_alignParentTop="true"
android:layout_marginRight="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView_pubDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<TextView
android:id="@+id/textViewHeadline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView_thumbnail"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView_pubDate"
android:layout_marginLeft="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewHeadline"
android:layout_toRightOf="@+id/imageView_thumbnail"
android:maxLines="@integer/Four"
android:layout_marginLeft="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</RelativeLayout>
I am displaying a list using a custom ListFragment. The fragment inflates
a layout and works fine. I want to add a horizontal scrollView above the
fragment to act as a category menu. It must be fixed, so addHeaderView is
not acceptable.
I found a layout example here on SO and plugged it into my layout to how
it could be made to work. But in its current form below, it does what you
would expect - puts a horizontal scrollview in every entry on the list.
So how do I share a screen with a static view on top and the listfragment
below.
Here is the layout that does not work - as I mentioned the Scrollview part
is not mine and is only included as an example. The relativelayout at the
bottom makes the actual list
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
<HorizontalScrollView
android:id="@+id/hor_svID"
android:layout_width="wrap_content"
android:layout_height="35dip"
android:scrollbars="none"
android:fillViewport="false"
android:focusable="false" >
<!--android:background="@drawable/submenu_bg"> -->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:focusable="false"
android:background="#FFFFFF"
android:gravity="center">
<TextView android:id="@+id/TechnologyTxtVId"
android:text="TECHNOLOGY"
android:textColor="#342D7E"
android:textSize="12sp"
android:textStyle="bold"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/SportsTxtVId"
android:text="SPORTS"
android:textColor="#342D7E"
android:textStyle="bold"
android:textSize="12sp"
android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:paddingLeft="15dip"></TextView>
<TextView android:id="@+id/EntntTxtVId"
android:text="ENTERTAINMENT"
android:textStyle="bold"
android:paddingLeft="15dip"
android:textSize="12sp"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/LocalTxtVId"
android:text="LOCAL"
android:textStyle="bold"
android:paddingLeft="15dip"
android:textSize="12sp"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/WorldTxtVId"
android:text="WORLD"
android:textStyle="bold"
android:textSize="12sp"
android:paddingLeft="15dip"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/FeaturesTxtVId"
android:text="FEATURES"
android:textStyle="bold"
android:textSize="12sp"
android:paddingLeft="15dip"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
<TextView android:id="@+id/RecentTxtVId"
android:text="RECENT"
android:textStyle="bold"
android:textSize="12sp"
android:paddingLeft="15dip"
android:gravity="center"
android:textColor="#342D7E"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
</HorizontalScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false" >
<ImageView
android:id="@+id/imageView_thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="@dimen/default_padding"
android:contentDescription="@null"
android:focusable="false"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView_source"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView_thumbnail"
android:layout_alignParentTop="true"
android:layout_marginRight="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView_pubDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
<TextView
android:id="@+id/textViewHeadline"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/imageView_thumbnail"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView_pubDate"
android:layout_marginLeft="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold" />
<TextView
android:id="@+id/textViewSummary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textViewHeadline"
android:layout_toRightOf="@+id/imageView_thumbnail"
android:maxLines="@integer/Four"
android:layout_marginLeft="@dimen/default_padding"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</RelativeLayout>
How to get JSON string from Android?
How to get JSON string from Android?
I made web service server application with .Net and this is my JSON output
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://stockcluster.com/">
{
"percentageArray":[85.0,13.0,2.0]
}
</string>
I want to know how to get this string from android. I tried myself but
"cannot convert to JSONObject from string" error occured.
My code is here:
double[] percentageArray = new double[3] {
lowPersentage,
normalPersentage,
highPersentage
};
json = JsonConvert.SerializeObject(new { percentageArray });
return json;
please help me !
I made web service server application with .Net and this is my JSON output
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://stockcluster.com/">
{
"percentageArray":[85.0,13.0,2.0]
}
</string>
I want to know how to get this string from android. I tried myself but
"cannot convert to JSONObject from string" error occured.
My code is here:
double[] percentageArray = new double[3] {
lowPersentage,
normalPersentage,
highPersentage
};
json = JsonConvert.SerializeObject(new { percentageArray });
return json;
please help me !
Tuesday, 1 October 2013
Slider Puzzle problems
Slider Puzzle problems
This is my slider puzzle game. So far it can only do 3x3 games. When I try
and pass the variables l and w (length and width) of the board it doesn't
work. It only works when i set the variables ROWS and COLS as finals. When
I try and change it I get errors. I'm not sure what to do, any help would
be appreciated.
Currently the user can input values that can't do anything at the moment.
When the game is started, a 3x3 board is generated. The user can restart
the game with a different scrambled board but the buttons that solve the
board and the buttons that reset the board to the original state do not
work yet.
public class SlidePuzzle {
public static void main(String[] args)
{
JFrame window = new JFrame("Slide Puzzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String length = JOptionPane.showInputDialog("Length");
String width = JOptionPane.showInputDialog("Width");
int l = Integer.parseInt(length);
int w = Integer.parseInt(width);
window.setContentPane(new SlidePuzzleGUI());
window.pack();
window.show();
window.setResizable(false);
}
}
public class SlidePuzzleGUI extends JPanel
{
private GraphicsPanel _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
This class contains the GUI for the Slider Puzzle
public SlidePuzzleGUI() {
JButton newGameButton = new JButton("New Game");
JButton resetButton = new JButton("Reset");
JButton solveButton = new JButton("I GIVE UP :(");
resetButton.addActionListener(new ResetAction());
newGameButton.addActionListener(new NewGameAction());
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(newGameButton);
controlPanel.add(resetButton);
controlPanel.add(solveButton);
_puzzleGraphics = new GraphicsPanel();
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}
This is the graphics panel
class GraphicsPanel extends JPanel implements MouseListener {
private static final int ROWS = 3;
private static final int COLS = 3;
private static final int CELL_SIZE = 80;
private Font _biggerFont;
public GraphicsPanel() {
_biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2);
this.setPreferredSize(
new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
this.setBackground(Color.black);
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
int x = c * CELL_SIZE;
int y = r * CELL_SIZE;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.gray);
g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
g.setColor(Color.black);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
}
}
}
}
public void mousePressed(MouseEvent e) {
int col = e.getX()/CELL_SIZE;
int row = e.getY()/CELL_SIZE;
if (!_puzzleModel.moveTile(row, col)) {
Toolkit.getDefaultToolkit().beep();
}
this.repaint();
}
public void mouseClicked (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}
public class NewGameAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}
public class ResetAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
_puzzleModel.tryAgain();
_puzzleGraphics.repaint();
}
}
public class solveAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
_puzzleModel.solve();
_puzzleGraphics.repaint();
}
}
}
public class SlidePuzzleModel {
private static final int ROWS = 3;
private static final int COLS = 3;
private Tile[][] _contents;
private Tile[][] _solved;
private Tile _emptyTile;
public SlidePuzzleModel() {
_contents = new Tile[ROWS][COLS];
reset();
}
String getFace(int row, int col) {
return _contents[row][col].getFace();
}
public void reset() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
_contents[r][c] = new Tile(r, c, "" + (r*COLS+c+1));
}
}
_emptyTile = _contents[ROWS-1][COLS-1];
_emptyTile.setFace(null);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
exchangeTiles(r, c, (int)(Math.random()*ROWS)
, (int)(Math.random()*COLS));
}
}
}
public void tryAgain()
{
}
public void solve()
{
for (int i = 1; i < ROWS+1;i++)
{
for(int j = 1; j < COLS+1;j++)
{
exchangeTiles(i, j, i, j);
}
}
}
public boolean moveTile(int r, int c) {
return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0)
|| checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1);
}
private boolean checkEmpty(int r, int c, int rdelta, int cdelta) {
int rNeighbor = r + rdelta;
int cNeighbor = c + cdelta;
if (isLegalRowCol(rNeighbor, cNeighbor)
&& _contents[rNeighbor][cNeighbor] == _emptyTile) {
exchangeTiles(r, c, rNeighbor, cNeighbor);
return true;
}
return false;
}
public boolean isLegalRowCol(int r, int c) {
return r>=0 && r<ROWS && c>=0 && c<COLS;
}
private void exchangeTiles(int r1, int c1, int r2, int c2) {
Tile temp = _contents[r1][c1];
_contents[r1][c1] = _contents[r2][c2];
_contents[r2][c2] = temp;
}
public boolean isGameOver() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<ROWS; c++) {
Tile trc = _contents[r][c];
return trc.isInFinalPosition(r, c);
}
}
return true;
}
}
class Tile {
private int _row;
private int _col;
private String _face;
public Tile(int row, int col, String face) {
_row = row;
_col = col;
_face = face;
}
public void setFace(String newFace) {
_face = newFace;
}
public String getFace() {
return _face;
}
public boolean isInFinalPosition(int r, int c) {
return r==_row && c==_col;
}
}
How would I make it so that the user can specify dimensions of the game
board?
EDIT public class SlidePuzzle {
public static void main(String[] args)
{
JFrame window = new JFrame("Slide Puzzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String length = JOptionPane.showInputDialog("Length");
String width = JOptionPane.showInputDialog("Width");
int l = Integer.parseInt(length);
int w = Integer.parseInt(width);
window.setContentPane(new SlidePuzzleGUI());
window.pack();
window.show();
window.setResizable(false);
}
} public class SlidePuzzleGUI extends JPanel {
private GraphicsPanel _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
public SlidePuzzleGUI(int l, int w) {
JButton newGameButton = new JButton("New Game");
JButton resetButton = new JButton("Reset");
JButton solveButton = new JButton("I GIVE UP :(");
resetButton.addActionListener(new ResetAction());
newGameButton.addActionListener(new NewGameAction());
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(newGameButton);
controlPanel.add(resetButton);
controlPanel.add(solveButton);
_puzzleGraphics = new GraphicsPanel(l,w);
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}
class GraphicsPanel extends JPanel implements MouseListener {
private int ROWS;
private int COLS;
private static final int CELL_SIZE = 80;
private Font _biggerFont;
public GraphicsPanel(int l, int w) {
_biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2);
this.setPreferredSize(
new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
this.setBackground(Color.black);
this.addMouseListener(this);
ROWS = l;
COLS = w;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
int x = c * CELL_SIZE;
int y = r * CELL_SIZE;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.gray);
g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
g.setColor(Color.black);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
}
}
}
}
public void mousePressed(MouseEvent e) {
int col = e.getX()/CELL_SIZE;
int row = e.getY()/CELL_SIZE;
if (!_puzzleModel.moveTile(row, col)) {
Toolkit.getDefaultToolkit().beep();
}
this.repaint();
}
public void mouseClicked (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}
public class NewGameAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}
public class ResetAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
_puzzleModel.tryAgain();
_puzzleGraphics.repaint();
}
}
public class solveAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
_puzzleModel.solve();
_puzzleGraphics.repaint();
}
}
}
This is my slider puzzle game. So far it can only do 3x3 games. When I try
and pass the variables l and w (length and width) of the board it doesn't
work. It only works when i set the variables ROWS and COLS as finals. When
I try and change it I get errors. I'm not sure what to do, any help would
be appreciated.
Currently the user can input values that can't do anything at the moment.
When the game is started, a 3x3 board is generated. The user can restart
the game with a different scrambled board but the buttons that solve the
board and the buttons that reset the board to the original state do not
work yet.
public class SlidePuzzle {
public static void main(String[] args)
{
JFrame window = new JFrame("Slide Puzzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String length = JOptionPane.showInputDialog("Length");
String width = JOptionPane.showInputDialog("Width");
int l = Integer.parseInt(length);
int w = Integer.parseInt(width);
window.setContentPane(new SlidePuzzleGUI());
window.pack();
window.show();
window.setResizable(false);
}
}
public class SlidePuzzleGUI extends JPanel
{
private GraphicsPanel _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
This class contains the GUI for the Slider Puzzle
public SlidePuzzleGUI() {
JButton newGameButton = new JButton("New Game");
JButton resetButton = new JButton("Reset");
JButton solveButton = new JButton("I GIVE UP :(");
resetButton.addActionListener(new ResetAction());
newGameButton.addActionListener(new NewGameAction());
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(newGameButton);
controlPanel.add(resetButton);
controlPanel.add(solveButton);
_puzzleGraphics = new GraphicsPanel();
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}
This is the graphics panel
class GraphicsPanel extends JPanel implements MouseListener {
private static final int ROWS = 3;
private static final int COLS = 3;
private static final int CELL_SIZE = 80;
private Font _biggerFont;
public GraphicsPanel() {
_biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2);
this.setPreferredSize(
new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
this.setBackground(Color.black);
this.addMouseListener(this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
int x = c * CELL_SIZE;
int y = r * CELL_SIZE;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.gray);
g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
g.setColor(Color.black);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
}
}
}
}
public void mousePressed(MouseEvent e) {
int col = e.getX()/CELL_SIZE;
int row = e.getY()/CELL_SIZE;
if (!_puzzleModel.moveTile(row, col)) {
Toolkit.getDefaultToolkit().beep();
}
this.repaint();
}
public void mouseClicked (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}
public class NewGameAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}
public class ResetAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
_puzzleModel.tryAgain();
_puzzleGraphics.repaint();
}
}
public class solveAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
_puzzleModel.solve();
_puzzleGraphics.repaint();
}
}
}
public class SlidePuzzleModel {
private static final int ROWS = 3;
private static final int COLS = 3;
private Tile[][] _contents;
private Tile[][] _solved;
private Tile _emptyTile;
public SlidePuzzleModel() {
_contents = new Tile[ROWS][COLS];
reset();
}
String getFace(int row, int col) {
return _contents[row][col].getFace();
}
public void reset() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
_contents[r][c] = new Tile(r, c, "" + (r*COLS+c+1));
}
}
_emptyTile = _contents[ROWS-1][COLS-1];
_emptyTile.setFace(null);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
exchangeTiles(r, c, (int)(Math.random()*ROWS)
, (int)(Math.random()*COLS));
}
}
}
public void tryAgain()
{
}
public void solve()
{
for (int i = 1; i < ROWS+1;i++)
{
for(int j = 1; j < COLS+1;j++)
{
exchangeTiles(i, j, i, j);
}
}
}
public boolean moveTile(int r, int c) {
return checkEmpty(r, c, -1, 0) || checkEmpty(r, c, 1, 0)
|| checkEmpty(r, c, 0, -1) || checkEmpty(r, c, 0, 1);
}
private boolean checkEmpty(int r, int c, int rdelta, int cdelta) {
int rNeighbor = r + rdelta;
int cNeighbor = c + cdelta;
if (isLegalRowCol(rNeighbor, cNeighbor)
&& _contents[rNeighbor][cNeighbor] == _emptyTile) {
exchangeTiles(r, c, rNeighbor, cNeighbor);
return true;
}
return false;
}
public boolean isLegalRowCol(int r, int c) {
return r>=0 && r<ROWS && c>=0 && c<COLS;
}
private void exchangeTiles(int r1, int c1, int r2, int c2) {
Tile temp = _contents[r1][c1];
_contents[r1][c1] = _contents[r2][c2];
_contents[r2][c2] = temp;
}
public boolean isGameOver() {
for (int r=0; r<ROWS; r++) {
for (int c=0; c<ROWS; c++) {
Tile trc = _contents[r][c];
return trc.isInFinalPosition(r, c);
}
}
return true;
}
}
class Tile {
private int _row;
private int _col;
private String _face;
public Tile(int row, int col, String face) {
_row = row;
_col = col;
_face = face;
}
public void setFace(String newFace) {
_face = newFace;
}
public String getFace() {
return _face;
}
public boolean isInFinalPosition(int r, int c) {
return r==_row && c==_col;
}
}
How would I make it so that the user can specify dimensions of the game
board?
EDIT public class SlidePuzzle {
public static void main(String[] args)
{
JFrame window = new JFrame("Slide Puzzle");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String length = JOptionPane.showInputDialog("Length");
String width = JOptionPane.showInputDialog("Width");
int l = Integer.parseInt(length);
int w = Integer.parseInt(width);
window.setContentPane(new SlidePuzzleGUI());
window.pack();
window.show();
window.setResizable(false);
}
} public class SlidePuzzleGUI extends JPanel {
private GraphicsPanel _puzzleGraphics;
private SlidePuzzleModel _puzzleModel = new SlidePuzzleModel();
public SlidePuzzleGUI(int l, int w) {
JButton newGameButton = new JButton("New Game");
JButton resetButton = new JButton("Reset");
JButton solveButton = new JButton("I GIVE UP :(");
resetButton.addActionListener(new ResetAction());
newGameButton.addActionListener(new NewGameAction());
JPanel controlPanel = new JPanel();
controlPanel.setLayout(new FlowLayout());
controlPanel.add(newGameButton);
controlPanel.add(resetButton);
controlPanel.add(solveButton);
_puzzleGraphics = new GraphicsPanel(l,w);
this.setLayout(new BorderLayout());
this.add(controlPanel, BorderLayout.NORTH);
this.add(_puzzleGraphics, BorderLayout.CENTER);
}
class GraphicsPanel extends JPanel implements MouseListener {
private int ROWS;
private int COLS;
private static final int CELL_SIZE = 80;
private Font _biggerFont;
public GraphicsPanel(int l, int w) {
_biggerFont = new Font("SansSerif", Font.BOLD, CELL_SIZE/2);
this.setPreferredSize(
new Dimension(CELL_SIZE * COLS, CELL_SIZE*ROWS));
this.setBackground(Color.black);
this.addMouseListener(this);
ROWS = l;
COLS = w;
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
for (int r=0; r<ROWS; r++) {
for (int c=0; c<COLS; c++) {
int x = c * CELL_SIZE;
int y = r * CELL_SIZE;
String text = _puzzleModel.getFace(r, c);
if (text != null) {
g.setColor(Color.gray);
g.fillRect(x+2, y+2, CELL_SIZE-4, CELL_SIZE-4);
g.setColor(Color.black);
g.setFont(_biggerFont);
g.drawString(text, x+20, y+(3*CELL_SIZE)/4);
}
}
}
}
public void mousePressed(MouseEvent e) {
int col = e.getX()/CELL_SIZE;
int row = e.getY()/CELL_SIZE;
if (!_puzzleModel.moveTile(row, col)) {
Toolkit.getDefaultToolkit().beep();
}
this.repaint();
}
public void mouseClicked (MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseEntered (MouseEvent e) {}
public void mouseExited (MouseEvent e) {}
}
public class NewGameAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
_puzzleModel.reset();
_puzzleGraphics.repaint();
}
}
public class ResetAction implements ActionListener {
@Override
public void actionPerformed(ActionEvent e) {
_puzzleModel.tryAgain();
_puzzleGraphics.repaint();
}
}
public class solveAction implements ActionListener
{
@Override
public void actionPerformed(ActionEvent e)
{
_puzzleModel.solve();
_puzzleGraphics.repaint();
}
}
}
Strings in C Issues
Strings in C Issues
I'm wondering what the difference is between
char* str[NUM];
char str[NUM];
char* str;
My understanding is that an array definition such as the one in the middle
points to the address of the first value, that is str[0]. But I also see
the notation represented by the first line of code, that is, a pointer to
an array. Are these three equivalent? I'm aware the array definition sets
space in the stack for NUM characters so is that probably better than just
char* str?
Thanks
I'm wondering what the difference is between
char* str[NUM];
char str[NUM];
char* str;
My understanding is that an array definition such as the one in the middle
points to the address of the first value, that is str[0]. But I also see
the notation represented by the first line of code, that is, a pointer to
an array. Are these three equivalent? I'm aware the array definition sets
space in the stack for NUM characters so is that probably better than just
char* str?
Thanks
Subscribe to:
Comments (Atom)