TicTacToe.java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.JOptionPane;
public class TicTacToe extends JFrame
{
JButton[][] buttons = new JButton[3][3];
JFrame frame = new JFrame("TicTacToe"); //Global frame
and grid button variables
JButton reset = new JButton("Reset"); //Create reset
button for game
JOptionPane turn;
int moveCounter = 9;
boolean gameWon = false;
int WhoseTurn = 1;
public TicTacToe() //Tic tac default constructor which
adds and dimensions Jframe
{
super();
frame.setSize(350, 355);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE); //Setting dimension
of Jframe and setting parameters
frame.setVisible(true);
frame.setResizable(false);
JButton[][] buttons = new
JButton[3][3];
JFrame frame = new
JFrame("TicTacToe"); //Global frame and grid button variables
JButton reset = new
JButton("Reset"); //Create reset button for game
JOptionPane turn;
}
private void checkWin(int row, int col)
{
try {
if
(buttons[0][2].getText()==buttons[1][2].getText()&&
buttons[1][2].getText()==buttons[2][2].getText()&&
buttons[2][2].getText()==buttons[0][2].getText()&&
buttons[1][2].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][2].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][2].getText()+ "
wins!!!");
}
if
(buttons[0][1].getText()==buttons[1][1].getText()&&
buttons[1][1].getText()==buttons[2][1].getText()&&
buttons[2][1].getText()==buttons[0][1].getText()&&
buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ "
wins!!!");
}
if
(buttons[0][0].getText()==buttons[1][0].getText()&&
buttons[1][0].getText()==buttons[2][0].getText()&&
buttons[2][0].getText()==buttons[0][0].getText()&&
buttons[1][0].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][0].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][0].getText()+ "
wins!!!");
}
if
(buttons[2][0].getText()==buttons[2][1].getText()&&
buttons[2][1].getText()==buttons[2][2].getText()&&
buttons[2][2].getText()==buttons[2][0].getText()&&
buttons[2][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[2][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[2][1].getText()+ "
wins!!!");
}
if
(buttons[1][0].getText()==buttons[1][1].getText()&&
buttons[1][1].getText()==buttons[1][2].getText()&&
buttons[1][2].getText()==buttons[1][0].getText()&&
buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ "
wins!!!");
}
if
(buttons[0][0].getText()==buttons[0][1].getText()&&
buttons[0][1].getText()==buttons[0][2].getText()&&
buttons[0][2].getText()==buttons[0][0].getText()&&
buttons[0][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[0][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[0][1].getText()+ "
wins!!!");
}
if
(buttons[0][0].getText()==buttons[1][1].getText()&&
buttons[1][1].getText()==buttons[2][2].getText()&&
buttons[2][2].getText()==buttons[0][0].getText()&&
buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ "
wins!!!");
}
if
(buttons[0][2].getText()==buttons[1][1].getText()&&
buttons[1][1].getText()==buttons[2][0].getText()&&
buttons[2][0].getText()==buttons[0][2].getText()&&
buttons[1][1].getText()!="")
{
gameWon = true;
WhoseTurn = 0;
System.out.println(buttons[1][1].getText()+ " wins!!!");
JOptionPane.showMessageDialog(frame, buttons[1][1].getText()+ "
wins!!!");
}
}catch(Exception e) {
gameWon = true;
WhoseTurn = 0;
System.out.println("Stalemate");
JOptionPane.showMessageDialog(frame, "Stalemate");
}
}
private void compTurn(int count)
{
int randomMove=count;
Random num = new
Random();
randomMove =
num.nextInt(randomMove)+1;
while(gameWon ==false &
WhoseTurn ==2)
{
for(int i = 0; i < 3; i++) //Create grid of buttons for tic tac
toe game
{
for(int j = 0; j < 3; j++)
{
if(buttons[i][j].isEnabled()==true)
{
randomMove--;
if(randomMove==0 )
{
buttons[i][j].setText("O");
buttons[i][j].setEnabled(false);
moveCounter--;
checkWin(i, j);
WhoseTurn = 1;
}
}
}
}
}
}
private void initialize() //Initialize tic tac toe
game board
{
JPanel mainPanel = new
JPanel(new BorderLayout()); //create main panel container to put
layer others on top
JPanel menu = new JPanel(new
BorderLayout());
JPanel game = new JPanel(new
GridLayout(3,3)); //Create two more panels with layouts for
buttons
frame.add(mainPanel); //add
main container panel to frame
mainPanel.setPreferredSize(new
Dimension(325,425));
menu.setPreferredSize(new
Dimension(300,50)); //Setting dimensions of panels
game.setPreferredSize(new
Dimension(300,300));
mainPanel.add(menu,
BorderLayout.NORTH); //Add two panels to the main container
panel
mainPanel.add(game,
BorderLayout.SOUTH);
//Add both start/reset buttons
to menu container panel
menu.add(reset,
BorderLayout.NORTH);
reset.addActionListener(new
myActionListener());
for(int i = 0; i < 3; i++)
//Create grid of buttons for tic tac toe game
{
for(int j = 0; j < 3; j++)
{
buttons[i][j] = new JButton(); //Instantiating buttons
buttons[i][j].setText("");
buttons[i][j].setVisible(true);
game.add(buttons[i][j]);
buttons[i][j].addActionListener(new myActionListener()); //Adding
response event to buttons
}
}
}
private class myActionListener implements
ActionListener
{ //Implementing action listener for buttons
public void
actionPerformed(ActionEvent a)
{
//Display X's or O's on the buttons
if(gameWon == false)
{
if(a.getSource() == buttons[0][0]) //Checking which button is
pressed
{
buttons[0][0].setText("X");
buttons[0][0].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(0,0);
}
else if(a.getSource() == buttons[0][1])
{
buttons[0][1].setText("X");
buttons[0][1].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(0,1);
}
else if(a.getSource() == buttons[1][0])
{
buttons[1][0].setText("X");
buttons[1][0].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(1,0);
}
else if(a.getSource() == buttons[1][1])
{
buttons[1][1].setText("X");
buttons[1][1].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(1,1);
}
else if(a.getSource() == buttons[1][2])
{
buttons[1][2].setText("X");
buttons[1][2].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(1,2);
}
else if(a.getSource() == buttons[2][2])
{
buttons[2][2].setText("X");
buttons[2][2].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(2,2);
}
else if(a.getSource() == buttons[0][2])
{
buttons[0][2].setText("X");
buttons[0][2].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(0,2);
}
else if(a.getSource() == buttons[2][1])
{
buttons[2][1].setText("X");
buttons[2][1].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(2,1);
}
else if(a.getSource() == buttons[2][0])
{
buttons[2][0].setText("X");
buttons[2][0].setEnabled(false);
WhoseTurn = 2;
moveCounter--;
compTurn(moveCounter);
checkWin(2,0);
}
}
if(a.getSource() == reset)
{
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
gameWon = false;
buttons[i][j].setText("");
buttons[i][j].setEnabled(true);
moveCounter = 9;
WhoseTurn = 1;
}
}
}
}
}
public static void main(String[] args)
{
TicTacToe game = new
TicTacToe(); //main method and instantiating tic tac object and
calling initialize function
game.initialize();
}
}
Output:

Battleship.java:
import java.util.Random;
import java.util.Scanner;
public class Battleship {
public static void main(String[] args) {
Random rand = new
Random();
Scanner sc=new
Scanner(System.in);
int row,col;
int[][] grid=new
int[6][6];
int correct_count=0;
char ch;
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++) {
grid[i][j]=0;
}
}
row = rand.nextInt(5) +
1;
for(int i=0;i<6;i++)
{
grid[row][i]=1;
}
for(int i=0;i<6;i++)
{
for(int j=0;j<6;j++) {
System.out.print(grid[i][j]+" ");
}
System.out.println();
}
while(correct_count<6)
{
System.out.print("Enter row number: ");
row=sc.nextInt();
System.out.print("Enter column number: ");
col=sc.nextInt();
if(grid[row][col]==1) {
correct_count++;
}
if(correct_count!=6) {
System.out.print("Do you want to continue(Y/N): ");
ch=sc.next().charAt(0);
ch=Character.toUpperCase(ch);
if(ch=='N') {
break;
}
}
}
if(correct_count==6) {
System.out.println("You win!!!!Congrats");
}
else {
System.out.println("You Lost!!!!");
}
}
}
Output:

The game Battleship is played on a grid board. Each opponent has multiple ships that are...
The game Battleship is played on a grid board. Each opponent has multiple ships that are placed on the grid where the other opponent cannot see them. In order to attack, each player takes turns calling out coordinates on a grid. If the attacker calls out a coordinate that hits their opponent's ship, they must call out, "Hit." You are going to be developing a computer program to mimic this game. Use the Gridlayout that is six columns by six...
For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...
Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...
Vankin’s Mile is a solitaire game played on an
n x m grid. using assebly language
Vankin's Mile is a solitaire game played on an n x m grid. The player starts by placing a token on any square of the grid. Then on each turn, the player moves the token either one square to the right or one square down. The game ends when player moves the token off the edge of the board. Each square of the grid...
1. The Pentagonal Game of Life is a variant of the Game of Life played on a grid in which each cell has 5 neighbours. Live cells are shown in grey and dead cells are shown in white. The rules are as follows Two cells are considered to be neighbours if they share an edge. A dead cell comes to life it if has exactly 2 living neighbours. .A live cell remains alive if it has 2 or 3 living...
can
someone solve this program using c++ (visual studio)?
You have a grid of 4x4 cells which is filled by numbers from 1 to 8. Each number appears twice in the grid. The purpose of the Memory Game is to find the matching numbers. To start the game, Player 1 chooses two cells to uncover the numbers behind them. If the numbers match, player 1 gets to go on and uncover two more numbers. If the numbers don't match, player...
Assignment overview In this assignment, you will implement a simple game of Battleship. If you are unfamiliar with the game Battleship, there are tutorials online describing the game. In short, there are two players that each have a 10 by 10 grid where ships are placed. The players take turns taking shots at each other’s ships. A player wins when they have shot at all spaces on their opponent’s grid that are occupied by ship. To sink a ship, you...
Tic Tac Toe Game: Help, please. Design and implement a console based Tic Tac Toe game. The objective of this project is to demonstrate your understanding of various programming concepts including Object Oriented Programming (OOP) and design. Tic Tac Toe is a two player game. In your implementation one opponent will be a human player and the other a computer player. ? The game is played on a 3 x 3 game board. ? The first player is known as...
Program 4: C++ The Game of War The game of war is a card game played by children and budding computer scientists. From Wikipedia: The objective of the game is to win all cards [Source: Wikipedia]. There are different interpretations on how to play The Game of War, so we will specify our SMU rules below: 1) 52 cards are shuffled and split evenly amongst two players (26 each) a. The 26 cards are placed into a “to play” pile...
C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...