gpt4 book ai didi

java - 正确使用图表

转载 作者:行者123 更新时间:2023-11-30 03:03:03 26 4
gpt4 key购买 nike

我想问一下我的这个项目使用什么是正确的UML图。这是一款安卓应用。UML 图:

  • 类图
  • 对象图
  • 用例图
  • 时序图
  • 协作图
  • 状态图
  • Activity 图
  • 组件图
  • 部署图

这是安卓应用程序代码(这是一个井字游戏):

主 Activity .java

package com.example.sample;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;

public class MainActivity extends Activity {

// Representing the game state:
private boolean noughtsTurn = false; // Who's turn is it? false=X true=O
private char board[][] = new char[3][3]; // for now we will represent the board as an array of characters

/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupOnClickListeners();
resetButtons();


}

/**
* Called when you press new game.
*
* @param view the New Game Button
*/
public void newGame(View view) {

noughtsTurn = false;
board = new char[3][3];
resetButtons();

}

/**
* Reset each button in the grid to be blank and enabled.
*/
private void resetButtons() {
TableLayout T = (TableLayout) findViewById(R.id.tableLayout);
for (int y = 0; y < T.getChildCount(); y++) {
if (T.getChildAt(y) instanceof TableRow) {
TableRow R = (TableRow) T.getChildAt(y);
for (int x = 0; x < R.getChildCount(); x++) {
if (R.getChildAt(x) instanceof Button) {
Button B = (Button) R.getChildAt(x);
B.setText("");
B.setEnabled(true);
}
}
}
}
TextView t = (TextView) findViewById(R.id.TextView);
t.setText(R.string.TextView);
}

/**
* Method that returns true when someone has won and false when nobody has.<br />
* It also display the winner on screen.
*
* @return
*/
private boolean checkWin() {

char winner = '\0';
if (checkWinner(board, 3, 'X')) {
winner = 'X';
} else if (checkWinner(board, 3, 'O')) {
winner = 'O';
}

if (winner == '\0') {
return false; // nobody won
} else {
// display winner
TextView T = (TextView) findViewById(R.id.TextView);
T.setText(winner + " wins");
return true;
}
}


/**
* This is a generic algorithm for checking if a specific player has won on a tic tac toe board of any size.
*
* @param board the board itself
* @param size the width and height of the board
* @param player the player, 'X' or 'O'
* @return true if the specified player has won
*/
private boolean checkWinner(char[][] board, int size, char player) {
// check each column
for (int x = 0; x < size; x++) {
int total = 0;
for (int y = 0; y < size; y++) {
if (board[x][y] == player) {
total++;
}
}
if (total >= size) {
return true; // they win
}
}

// check each row
for (int y = 0; y < size; y++) {
int total = 0;
for (int x = 0; x < size; x++) {
if (board[x][y] == player) {
total++;
}
}
if (total >= size) {
return true; // they win
}
}

// forward diag
int total = 0;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (x == y && board[x][y] == player) {
total++;
}
}
}
if (total >= size) {
return true; // they win
}

// backward diag
total = 0;
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
if (x + y == size - 1 && board[x][y] == player) {
total++;
}
}
}
if (total >= size) {
return true; // they win
}

return false; // nobody won
}

/**
* Disables all the buttons in the grid.
*/
private void disableButtons() {
TableLayout T = (TableLayout) findViewById(R.id.tableLayout);
for (int y = 0; y < T.getChildCount(); y++) {
if (T.getChildAt(y) instanceof TableRow) {
TableRow R = (TableRow) T.getChildAt(y);
for (int x = 0; x < R.getChildCount(); x++) {
if (R.getChildAt(x) instanceof Button) {
Button B = (Button) R.getChildAt(x);
B.setEnabled(false);
}
}
}
}
}

/**
* This will add the OnClickListener to each button inside out TableLayout
*/
private void setupOnClickListeners() {
TableLayout T = (TableLayout) findViewById(R.id.tableLayout);
for (int y = 0; y < T.getChildCount(); y++) {
if (T.getChildAt(y) instanceof TableRow) {
TableRow R = (TableRow) T.getChildAt(y);
for (int x = 0; x < R.getChildCount(); x++) {
View V = R.getChildAt(x); // In our case this will be each button on the grid
V.setOnClickListener(new PlayOnClick(x, y));
}
}
}
}

/**
* Custom OnClickListener for Noughts and Crosses<br />
* Each Button for Noughts and Crosses has a position we need to take into account
*
* @author Lyndon Armitage
*/
private class PlayOnClick implements View.OnClickListener {

private int x = 0;
private int y = 0;

public PlayOnClick(int x, int y) {
this.x = x;
this.y = y;
}

@Override
public void onClick(View view) {
if (view instanceof Button) {
Button B = (Button) view;
board[x][y] = noughtsTurn ? 'O' : 'X';
B.setText(noughtsTurn ? "O" : "X");
B.setEnabled(false);
noughtsTurn = !noughtsTurn;

// check if anyone has won
if (checkWin()) {
disableButtons();
}
}
}
}
}

最佳答案

  • 用于范围定义的用例图。
  • 用于定义使用逻辑的状态机/图表
  • 用于开始规划类和源代码放置的包图
  • 支持逻辑的类的类图
  • 所用算法的序列图
  • 用于规划复杂类的复合结构图。

如果一些图表是绝对明显的,你不需要这样做。

但是!在编码之后你不需要图表,而是好的 javadocs。

如果这不是您的代码,而您想理解它,图表可以提供帮助。从画比较简单的开始,一直画到你最了解的那些。

如果这是您的代码并且您想要更改它,请在您想要重构的概念上绘制图表。

关于java - 正确使用图表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22273017/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com