📄 monopolyboard.java
字号:
else { Player2.Move( Dice1 + Dice2, snakeEyes, propertymanager ); String a2 = propertymanager.CheckProperty( Player2.PlayerNum, Player2.Position ); } repaint(); } //either the 'buy' button was clicked or the 'b' button was typed public void Buy() { // if they have enough money, sell it and set it as being owned if (propertymanager.Properties[Player1.Position][0] == 2)// propertymanager.Properties[Player1.Position][0] = 2; else if (propertymanager.Properties[Player2.Position][0] == 1) propertymanager.Properties[Player1.Position][0] = 1; else if( turn && Player1.Money>=propertymanager.Properties[Player1.Position][8] && propertymanager.Properties[Player1.Position][0] != -1 && propertymanager.Properties[Player1.Position][0] != 1 ) { propertymanager.Properties[Player1.Position][0] = 1; Player1.Money -= propertymanager.Properties[Player1.Position][8];System.out.println( Player1.Name + " paid $" + propertymanager.Properties[Player1.Position][8] + " for "+ propertymanager.List[Player1.Position] ); if( Player1.Position == 5 || Player1.Position == 15 || Player1.Position == 25 || Player1.Position == 35 ) { int count = 0; //fix the railroad price according to the number owned if( propertymanager.Properties[5][0] == 1 ) count++; if( propertymanager.Properties[15][0] == 1 ) count++; if( propertymanager.Properties[25][0] == 1 ) count++; if( propertymanager.Properties[35][0] == 1 ) count++; if( count == 1 ) count = 25; else if( count == 2 ) count = 50; else if( count == 3 ) count = 100; else if( count == 4 ) count = 200; if( propertymanager.Properties[5][0] == 1 ) propertymanager.Properties[5][2] = count; if( propertymanager.Properties[15][0] == 1 ) propertymanager.Properties[15][2] = count; if( propertymanager.Properties[25][0] == 1 ) propertymanager.Properties[25][2] = count; if( propertymanager.Properties[35][0] == 1 ) propertymanager.Properties[35][2] = count; } }//do the same for player 2 else if( !turn && Player2.Money>=propertymanager.Properties[Player2.Position][8] && propertymanager.Properties[Player2.Position][0] != -1 && propertymanager.Properties[Player2.Position][0] != 2 ) { propertymanager.Properties[Player2.Position][0] = 2; Player2.Money -= propertymanager.Properties[Player2.Position][8];System.out.println( Player2.Name + " paid $" + propertymanager.Properties[Player2.Position][8] + " for "+ propertymanager.List[Player2.Position] ); if( Player2.Position == 5 || Player2.Position == 15 || Player2.Position == 25 || Player2.Position == 35 ) { int count = 0; if( propertymanager.Properties[5][0] == 2 ) count++; if( propertymanager.Properties[15][0] == 2 ) count++; if( propertymanager.Properties[25][0] == 2 ) count++; if( propertymanager.Properties[35][0] == 2 ) count++; if( count == 1 ) count = 25; else if( count == 2 ) count = 50; else if( count == 3 ) count = 100; else if( count == 4 ) count = 200; if( propertymanager.Properties[5][0] == 2 ) propertymanager.Properties[5][2] = count; if( propertymanager.Properties[15][0] == 2 ) propertymanager.Properties[15][2] = count; if( propertymanager.Properties[25][0] == 2 ) propertymanager.Properties[25][2] = count; if( propertymanager.Properties[35][0] == 2 ) propertymanager.Properties[35][2] = count; } } repaint(); } //either the 'add' button was clicked or the 'a' button was typed public void Add() { final Frame fr = new Frame( "Add Houses" ); final Choice choice = new Choice(); for( int i=0; i<40; i++ ) { //check to see if all three of the properties are owned, if so, add it //to the list of possible house additions if(propertymanager.Properties[i][11] != -1 && propertymanager.Properties[i][12] != -1 ) { if( turn && propertymanager.Properties[i][0] == 1 && propertymanager.Properties[propertymanager.Properties[i][11]][0] == 1 && (propertymanager.Properties[propertymanager.Properties[i][12]][0] == 1 || propertymanager.Properties[i][12] == -1) && i!=12 && i!=28) choice.add( propertymanager.List[i] ); else if( !turn && propertymanager.Properties[i][0] == 2 && propertymanager.Properties[propertymanager.Properties[i][11]][0] == 2 && (propertymanager.Properties[propertymanager.Properties[i][12]][0] == 2 || propertymanager.Properties[i][12] == -1)&& i!=12 && i!=28) choice.add( propertymanager.List[i] ); } } final TextField howMany = new TextField( "1", 1 ); final Label l = new Label( "How Many:" ); final Button done = new Button( "Done" ); final Button go = new Button( "Add the House(s)" ); fr.setLayout( new FlowLayout( FlowLayout.CENTER ) ); fr.setBackground( Color.lightGray ); fr.setLayout( new GridLayout( 2, 3) ); fr.setSize( 400, 170 ); fr.add( choice ); fr.add( l, Label.CENTER ); fr.add( howMany ); fr.add( go ); fr.add( done ); fr.setVisible( true ); done.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { fr.setVisible( false ); } }); go.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int amt = Integer.parseInt( howMany.getText() ); int selected = 0; for( int i=0; i<40; i++ ) if( choice.getSelectedItem() == propertymanager.List[i] ) selected = i; if( turn ) { //add the house(s) and take the money if( propertymanager.Properties[selected][11] != -1 && propertymanager.Properties[selected][12] != -1 ) if( propertymanager.Properties[selected][1] < 5 && propertymanager.Properties[propertymanager.Properties[selected][11]][0] == 1 && (propertymanager.Properties[propertymanager.Properties[selected][12]][0] == 1 || propertymanager.Properties[selected][12] == -1) && (selected != 12 || selected != 28)) { int count=0; while( count < amt && propertymanager.Properties[selected][1] < 5 && Player1.Money>=propertymanager.Properties[selected][10] ) { propertymanager.Properties[selected][1]++; Player1.Money-=propertymanager.Properties[selected][10]; count++; } } } else {//do same for player 2 if( propertymanager.Properties[selected][11] != -1 && propertymanager.Properties[selected][12] != -1 ) if( propertymanager.Properties[selected][1] < 5 && propertymanager.Properties[propertymanager.Properties[selected][11]][0] == 2 && (propertymanager.Properties[propertymanager.Properties[selected][12]][0] == 2 || propertymanager.Properties[selected][12] == -1) && (selected != 12 || selected != 28)) { int count=0; while( count < amt && propertymanager.Properties[selected][1] < 5 && Player2.Money>=propertymanager.Properties[selected][10] ) { propertymanager.Properties[selected][1]++; Player2.Money-=propertymanager.Properties[selected][10]; count++; } } } repaint(); } }); } //either the 'remove' button was clicked or the 'r' button was typed public void Rem() { final Choice choice = new Choice(); fr.setBackground( Color.lightGray ); fr.setSize( 400, 170 ); //if the property is owned, add it to the list for( int i=0; i<40; i++ ) { if( turn && propertymanager.Properties[i][0] == 1 ) choice.add( propertymanager.List[i] ); else if( !turn && propertymanager.Properties[i][0] == 2 ) choice.add( propertymanager.List[i] ); } //if they don't have any money and they have no property to sell, they lose if( noMoney && choice.getItemCount() == 0 && !fr.isShowing() ) { if( turn ) fr.add( new Label( "You Lose " + Player1.Name + "." ) ); else fr.add( new Label( "You Lose " + Player2.Name + "." ) ); fr.setVisible( true ); fr.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { fr.setVisible( false ); } }); } else { final TextField howMany = new TextField( "1", 1 ); final Label l = new Label( "How Many:" ); final Button done = new Button( "Done" ); final Button go = new Button( "Remove the House(s)" ); final Button sell = new Button("Sell the Property"); fr.setLayout( new GridLayout( 2, 3) ); fr.add( choice ); fr.add( l, Label.CENTER ); fr.add( howMany ); fr.add( go ); fr.add( sell ); fr.add( done ); fr.setVisible( true ); done.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { if( frame.isShowing() ) frame.setVisible( false ); fr.setVisible( false ); fr.remove( choice ); fr.remove( l ); fr.remove( howMany ); fr.remove( go ); fr.remove( sell ); fr.remove( done ); } }); sell.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { //sell the houses and add the money to the persons .Money int selected = 0; for( int i=0; i<40; i++ ) if( choice.getSelectedItem() == propertymanager.List[i] ) selected = i; if( selected == 5 ) { if( turn ){ Player1.Money+=100; } else { Player2.Money+=100; } propertymanager.Properties[selected][0] = 0; } else if( selected == 15 ) { if( turn ) { Player1.Money+=100; } else { Player2.Money+=100; } propertymanager.Properties[selected][0] = 0; } else if( selected == 25 ) { if( turn ) { Player1.Money+=100; } else { Player2.Money+=100; } propertymanager.Properties[selected][0] = 0; } else if( selected == 35 ) { if( turn ) { Player1.Money+=100; } else { Player2.Money+=100; } propertymanager.Properties[selected][0] = 0; } else if( selected == 12 || selected == 28 ) { if( turn ) { Player1.Money+=75; } else { Player2.Money+=75; } } else if( propertymanager.Properties[selected][1] == 0 || propertymanager.Properties[selected][1] == -1 ) { propertymanager.Properties[selected][0] = 0; if( turn ) { Player1.Money+=propertymanager.Properties[selected][10]; } else { Player2.Money+=propertymanager.Properties[selected][10]; } } choice.remove( propertymanager.List[selected] ); repaint(); } }); go.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { int amt = Integer.parseInt( howMany.getText() ); int selected = 0; for( int i=0; i<40; i++ ) if( choice.getSelectedItem() == propertymanager.List[i] ) selected = i; if( turn ) { if( propertymanager.Properties[selected][1] > 0 ) { int count=0; //remove the house(s) and add the money while( count < amt && propertymanager.Properties[selected][1] > 0 ) { propertymanager.Properties[selected][1]--; Player1.Money+=propertymanager.Properties[selected][10];//System.out.println( Player1.Name + " gained $" + propertymanager.Properties[selected][10] + " for selling houses on " + propertymanager.List[selected] ); count++; } } } else { if( propertymanager.Properties[selected][1] > 0 ) { int count=0; while( count < amt && propertymanager.Properties[selected][1] > 0 ) { propertymanager.Properties[selected][1]--; Player2.Money+=propertymanager.Properties[selected][10];//System.out.println( Player2.Name + " gained $" + propertymanager.Properties[selected][10] + " for selling houses on " + propertymanager.List[selected] ); count++; } } } repaint(); } }); } } //start a new game public void New() { final Frame a = new Frame(); final TextField tf1 = new TextField("Player 1", 20 ); final TextField tf2 = new TextField("Player 2", 20 ); final Button b = new Button( "Start the Game!"); a.setSize( 400, 110 ); a.setBackground( Color.lightGray ); a.setLayout( new FlowLayout(FlowLayout.CENTER) ); a.add( tf1); a.add( b ); a.add( tf2 ); a.setVisible( true ); b.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent e ) { Player1.Name = tf1.getText(); Player2.Name = tf2.getText(); a.setVisible( false ); Player1.Position = 0; Player2.Position = 0; Player1.Money = 1500; Player2.Money = 1500; Player1.inJail = false; Player2.inJail = false; noMoney = false; turn = true; rolled = false; for( int i=0; i<40; i++ ) { if( propertymanager.Properties[i][0] != -1 ) propertymanager.Properties[i][0] = 0; if( propertymanager.Properties[i][1] != -1 ) propertymanager.Properties[i][1] = 0; } propertymanager.Properties[20][1] = 500; repaint(); } }); a.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ) { a.setVisible( false ); repaint(); } }); repaint(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -