Oopm Mini Project Report
Oopm Mini Project Report
AIM:
The basic aim of this project is to create awareness and enhance the knowledge in the
subject.By creating this mini project one could learn the core concepts involved in the subject
and the real life applications of OOPM.One can strengthen the concepts of oopm via this mini-
project.The prime aim of creating this game "Connect-Four" is that it covers major concepts in
the subject JAVA.It is like a checkpoint in the learning process. Creating this game safeguards
your knowledge in JAVA and builds in the confidence to learn further.
INTRODUCTION:
PROPOSE APPLICATION:
CONCLUSION:
Through this mini project work we learnt great concepts of Java used in a real life game
application.
We even learnt about-”Game Theory” and its application in this project.
This project has imbibed the knowledge of OOPM and real life application.Various
concepts of Java were learnt apart from the regular theoretical ones.This project was a huge
success and hence embedded coding for fun in our lives.
CODE:
Main.java:
package miniproject;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;
@Override
public void start(Stage primaryStage) throws Exception {
controller = loader.getController();
controller.createPlayground();
primaryStage.setScene(scene);
primaryStage.setTitle("Connect Four");
primaryStage.setResizable(false);
primaryStage.show();
}
private MenuBar createMenu() {
// File Menu
Menu fileMenu = new Menu("File");
// Help Menu
Menu helpMenu = new Menu("Help");
return menuBar;
}
alert.show();
}
Platform.exit();
System.exit(0);
}
import javafx.animation.TranslateTransition;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Point2D;
import javafx.scene.control.*;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.shape.Shape;
import javafx.util.Duration;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@FXML
public GridPane rootGridPane;
@FXML
public Pane insertedDiscsPane;
@FXML
public Button setNameButton;
@FXML
public TextField player1Name;
@FXML
public TextField player2Name;
List<Rectangle> rectangleList=createClickableColumns();
for (Rectangle rectangle:rectangleList ) {
rootGridPane.add(rectangle,0,1);
rectangleWithHoles.setFill(Color.WHITE);
return rectangleWithHoles;
}
rectangle.setOnMouseClicked(event -> {
if (isAllowedToEnter)
{
isAllowedToEnter=false;
insertDisc(new Disc(isPlayerOneTurn), column);
}
});
rectangleList.add(rectangle);
}
return rectangleList;
}
int row=ROWS-1;
while(row>=0){
if(getDiscIfPresent(row,column)==null){
break;
}
row--;
}
if (row<0){
return;
}
insertedDiscArray[row][column]=disc;
insertedDiscsPane.getChildren().add(disc);
disc.setTranslateX(column * (CIRCLE_DIAMETER + 5) + CIRCLE_DIAMETER / 4);
TranslateTransition translateTransition=new
TranslateTransition(Duration.seconds(0.6),disc);
isAllowedToEnter=true;
if(gameEnded(currentRow,column)){
gameOver();
return;
}
isPlayerOneTurn=!isPlayerOneTurn;
playerNameLabel.setText(isPlayerOneTurn?PLAYER_ONE:PLAYER_TWO);
});
translateTransition.play();
return isEnded;
}
if (disc!=null&&disc.isPlayerOneMove==isPlayerOneTurn){
chain++;
if (chain==4){
return true;
}
}else {
chain=0;
}
}
return false;
}
private Disc getDiscIfPresent(int row,int column){
if (row>=ROWS||row<0||column>=COLUMNS||column<0)
return null;
return insertedDiscArray[row][column];
}
private void gameOver() {
String winner=isPlayerOneTurn?PLAYER_ONE:PLAYER_TWO;
System.out.println("Winner is "+winner);
Alert alert=new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Connect Four");
alert.setHeaderText("Winner is"+winner);
alert.setContentText("Want to play again");
alert.getButtonTypes().setAll(yesButton,noButton);
Platform.runLater(()->{
Optional<ButtonType>clickedButton=alert.showAndWait();
if (clickedButton.isPresent()&&clickedButton.get()==yesButton){
resetGame();
}else{
exitGame();
}
});
insertedDiscsPane.getChildren().clear();
for (int row=0;row< insertedDiscArray.length;row++){
for (int col=0;col<insertedDiscArray[row].length;col++){
insertedDiscArray[row][col]=null;
}
}
isPlayerOneTurn=true;
playerNameLabel.setText(PLAYER_ONE);
createPlayground();
Platform.exit();
System.exit(0);
}
@Override
public void initialize(URL location, ResourceBundle resources) {
setNameButton.setOnAction(event -> {
PLAYER_ONE=player1Name.getText();
PLAYER_TWO=player2Name.getText();
});
}
}
game.fxml:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.Pane?>
<?import javafx.scene.layout.Region?>
<?import javafx.scene.layout.RowConstraints?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>
<GridPane fx:id="rootGridPane" style="-fx-background-color: #D9F7F0;"
xmlns="http://javafx.com/javafx/8.0.221" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="miniproject.Controller">
<columnConstraints>
<ColumnConstraints />
<ColumnConstraints maxWidth="298.0" minWidth="225.0" prefWidth="225.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" prefHeight="30.0" />
<RowConstraints />
</rowConstraints>
<children>
<Pane GridPane.columnSpan="2" />
<Pane fx:id="insertedDiscsPane" prefHeight="400.0" prefWidth="200.0"
GridPane.rowIndex="1" />
<VBox style="-fx-background-color: #2B3B4C;" GridPane.columnIndex="1"
GridPane.rowIndex="1">
<children>
<Pane prefHeight="141.0" prefWidth="302.0">
<children>
<TextField fx:id="player1Name" layoutX="21.0" layoutY="34.0" prefHeight="25.0"
prefWidth="185.0" promptText="Player 1 name" />
<TextField fx:id="player2Name" layoutX="22.0" layoutY="58.0" prefHeight="25.0"
prefWidth="185.0" promptText="Player 2 name" />
<Button fx:id="setNameButton" layoutX="22.0" layoutY="88.0"
mnemonicParsing="false" prefHeight="25.0" prefWidth="185.0" text="Set Name" />
</children>
</Pane>
<Region prefHeight="134.0" prefWidth="302.0" VBox.vgrow="ALWAYS" />
<Label fx:id="playerNameLabel" alignment="CENTER" prefHeight="35.0"
prefWidth="302.0" text="Player One" textFill="WHITE">
<font>
<Font name="System Bold" size="29.0" />
</font>
</Label>
<Label alignment="CENTER" prefHeight="31.0" prefWidth="301.0" text="Turn"
textFill="WHITE">
<font>
<Font size="26.0" />
</font>
</Label>
<Region prefHeight="200.0" prefWidth="200.0" VBox.vgrow="ALWAYS" />
</children>
</VBox>
</children>
</GridPane>
OUTPUT: